• gui.OpenUrl on Chat Message
    26 replies, posted
I am trying to open a webpage on the steam browser when people say a certain thing in chat, but when I use [lua] function Google(ply, text) if (string.sub("!google", 1, 7)) then gui.OpenURL("Google.com") end end hook.Add("OnPlayerChat", "Google", Google) [/lua] Nothing is produced at all..I put it under a clientside file since gui.OpenURL and OnPlayerChat are both ran clientside. Any help?
What are you comparing "!google" to?
Here, I think this is what he wanted. The only thing that was wrong is that you didn't added which protocol was. [lua] function Google(pl, text) if string.find(text, "!google") then gui.OpenURL("http://google.com/#hl=en&output=search&q=" ..string.sub(text, 9)) return true end end hook.Add("OnPlayerChat", "Google", Google) [/lua] If you don't want the string.find then you can also use this, which is pretty much almost the same. But then again, you want to check the first 7 characters instead of being popped up from the sentence. [lua]if string.sub(text, 1, 7) == "!google" then[/lua]
[QUOTE=ptown2;39772621]Here, I think this is what he wanted. The only thing that was wrong is that you didn't added which protocol was. [lua] function Google(pl, text) if string.find(text, "!google") then gui.OpenURL("http://google.com/#hl=en&output=search&q=" ..string.sub(text, 9)) return true end end hook.Add("OnPlayerChat", "Google", Google) [/lua] If you don't want the string.find then you can also use this, which is pretty much almost the same. But then again, you want to check the first 7 characters instead of being popped up from the sentence. [lua]if string.sub(text, 1, 7) == "!google" then[/lua][/QUOTE] Thanks a lot, but also what is the need for the [QUOTE]..string.sub(text, 9)[/QUOTE] if you don't mind me asking questions.
[QUOTE=Gaming_Unlim;39772852]Thanks a lot, but also what is the need for the if you don't mind me asking questions.[/QUOTE] That's the part for the search query. It will copy all of that text within that position and further. Examples (think them without the quotes): [code] "!google" - Opens google search without doing any search "!google " - Opens google search with the query being a space. Google will auto-delete it. "!google lua" - Opens a google search with the query to "lua". "!googlelua" - STILL opens a google search with the query to "ua". Why? Because you didn't left the space it needed. [/code]
It seems that it opens for my whole server when I try this, anyway to make it the player only?
[QUOTE=kila58;39773036]It seems that it opens for my whole server when I try this, anyway to make it the player only?[/QUOTE] Looks like playing this in singleplayer was a bad idea. Here, place this line above the gui.OpenUrl line: [lua] if not pl == LocalPlayer() then return true end [/lua]
I tried this on darkrp and it opens the url on anyone that is in range with you.
[QUOTE=Gaming_Unlim;39779993]I tried this on darkrp and it opens the url on anyone that is in range with you.[/QUOTE] Install ulx. [code]CATEGORY_NAME = "Donate" // Donate function ulx.donate(ply) ply:SendLua([[gui.OpenURL("donatelink")]]) end local donate = ulx.command( CATEGORY_NAME, "ulx donate", ulx.donate, "!donate" ) donate:defaultAccess( ULib.ACCESS_ALL ) donate:help( "Donate $10 or more to become a lifetime VIP Status." ) [/code] ... Profit.
[QUOTE=ptown2;39772621]Here, I think this is what he wanted. The only thing that was wrong is that you didn't added which protocol was. [lua] function Google(pl, text) if string.find(text, "!google") then gui.OpenURL("http://google.com/#hl=en&output=search&q=" ..string.sub(text, 9)) return true end end hook.Add("OnPlayerChat", "Google", Google) [/lua] If you don't want the string.find then you can also use this, which is pretty much almost the same. But then again, you want to check the first 7 characters instead of being popped up from the sentence. [lua]if string.sub(text, 1, 7) == "!google" then[/lua][/QUOTE] Will this work with a html file as well? Like if the url was [url]www.something/myfile.html[/url] ? If I remove the search string part ofc
[QUOTE=Gaming_Unlim;39779993]I tried this on darkrp and it opens the url on anyone that is in range with you.[/QUOTE] Are you sure you applied the extra line (the pl == LocalPlayer()), considering it is clientsided.
[lua] if SERVER then AddCSLuaFile(ChatToWebsite.lua) end function ChatToWebsite(pl, text) if string.sub(text, 1, 6) == "!rules" then if not pl == LocalPlayer() then return true end gui.OpenURL("https://blabla.com/motd.html") return true end end hook.Add("OnPlayerChat", "ChatToWebsite", ChatToWebsite) [/lua] This produces nothing. What did I miss?
You're trying to use a variable called lua inside the table ChatToWebsite on line 2. It has to be a string - the quotes aren't there for nothing. Plus, just calling AddCSLuaFile without a string will do it on the current file. "if not pl == LocalPlayer() then return true end" is basically doing "if (not pl) == LocalPlayer() ...", which ends up being "if false == LocalPlayer() ..." The function also doesn't need to be global, and I added a return at the top because the server doesn't need to go past that. /nitpicking [lua] if SERVER then AddCSLuaFile() return end local function ChatToWebsite(pl, text) if string.sub(text, 1, 6) == "!rules" then if pl != LocalPlayer() then return true end gui.OpenURL("https://blabla.com/motd.html") return true end end hook.Add("OnPlayerChat", "ChatToWebsite", ChatToWebsite)[/lua]
Thanks for a good awnser, Will test this out, and report back! EDIT: just to be clear, this goes in lua/autorun right?
If I wanted something like this to only work for a certain player how would I do that?
What kind of player?
[QUOTE=mib999;39811449]What kind of player?[/QUOTE] Like if I wanted it to be so that only I could open a menu with chat and no one else how would I do it?
[lua]if pl:SteamID()~="STEAM_0:0:123456" then return end[/lua] Right after the ChatToWebsite function start.
[QUOTE=.\\Shadow};39805799]You're trying to use a variable called lua inside the table ChatToWebsite on line 2. It has to be a string - the quotes aren't there for nothing. Plus, just calling AddCSLuaFile without a string will do it on the current file. "if not pl == LocalPlayer() then return true end" is basically doing "if (not pl) == LocalPlayer() ...", which ends up being "if false == LocalPlayer() ..." The function also doesn't need to be global, and I added a return at the top because the server doesn't need to go past that. /nitpicking [lua] if SERVER then AddCSLuaFile() return end local function ChatToWebsite(pl, text) if string.sub(text, 1, 6) == "!rules" then if pl != LocalPlayer() then return true end gui.OpenURL("https://blabla.com/motd.html") return true end end hook.Add("OnPlayerChat", "ChatToWebsite", ChatToWebsite)[/lua][/QUOTE] Thanks again for your help, but all that happens when i type "!rules" is that nothing is returned in chat. This script is supposed to go in garrysmod/lua/autorun right?
anyone knows if this is still doable or if Garry changed something?
[QUOTE=zapha;39810131]Thanks for a good awnser, Will test this out, and report back! EDIT: just to be clear, this goes in lua/autorun right?[/QUOTE] Yesh, thats where you put all your custom goodies.
Well, then I cant say that this piece of lua works. I've seen very similar scripts before, so maybe Garry changed something.
-snip- after rereading what I posted it isn't going to work any differently from the previously posted one.
As I said, I've seen similar scripts before, like [URL="http://facepunch.com/showthread.php?t=1239335"]this[/URL] So maybe something has been done to gmod. I get no errors with the script, but nothing is returned or happens
[QUOTE=zapha;39860285]As I said, I've seen similar scripts before, like [URL="http://facepunch.com/showthread.php?t=1239335"]this[/URL] So maybe something has been done to gmod. I get no errors with the script, but nothing is returned or happens[/QUOTE] Use this to check if it gets called. [LUA] if SERVER then AddCSLuaFile() return end local function ChatToWebsite(ply, txt) if (ply == LocalPlayer() and string.sub(txt, 1, 6) == "!rules") then MsgN("Hook worked.") gui.OpenURL("http://google.com") return true end end hook.Add("OnPlayerChat", "ChatToWebsite", ChatToWebsite) [/LUA] You might also want to check if the problem is using https instead of http.
In-case anyone wants to lern. [lua]local tCommand = {}; local sCommandPrefix = "!"; -- What commands start with. -- For aesthetic poopie. local function AddCommand( sCommand, fFunction ) tCommand[ string.lower( sCommand ) ] = fFunction; end -- The first command. AddCommand( "google", function( ePlayer, tArguments ) -- Because we don't want to include the command in the g00gle surch. tArguments[1] = ""; if ( #tArguments > 1 ) then -- We're getting rid of the first '+' and concatting the arguments into the url. lelele gui.OpenURL( "http://google.com/#hl=en&output=search&q=" .. string.sub( table.concat( tArguments, "+" ), 2 ) ); end end ); -- Add the hook. hook.Add( "OnPlayerChat", "ChatCommandPlugin", function( ePlayer, sText ) -- If the person speaking isn't us, we'll just stop right here. if ( ePlayer ~= LocalPlayer() ) then return; end -- Check the prefix of the message, to see if it's a command or not. if ( string.StartWith( sText, sCommandPrefix ) ) then -- Split the message local tArguments = string.Split( sText, " " ); if ( #tArguments > 0 ) then -- Make the first argument (The command) lowercase and remove the prefix. tArguments[1] = string.lower( string.sub( tArguments[1], 1 + string.len( sCommandPrefix ) ) ); -- Check if the command exists. if ( tCommand[ tArguments[1] ] ~= nil ) then -- Call the command's function and provide the player and arguments. tCommand[ tArguments[1] ]( ePlayer, tArguments ); end end end end );[/lua]
[QUOTE=isnipeu;39860729]Use this to check if it gets called. [LUA] if SERVER then AddCSLuaFile() return end local function ChatToWebsite(ply, txt) if (ply == LocalPlayer() and string.sub(txt, 1, 6) == "!rules") then MsgN("Hook worked.") gui.OpenURL("http://google.com") return true end end hook.Add("OnPlayerChat", "ChatToWebsite", ChatToWebsite) [/LUA] You might also want to check if the problem is using https instead of http.[/QUOTE] Dont know what I did wrong, but using your script worked! I might have had a typo or something somewhere. [QUOTE]In-case anyone wants to lern.[/QUOTE] Dude, that seems kind of overkill.
Sorry, you need to Log In to post a reply to this thread.