• How To Make Chat Commands?
    15 replies, posted
Hello, I'm currently developing a suggestion for a Gmod Community. I have done the first part which is a Derma, but the second part is chat commands and I'm not too sure how to make them. Does anyone know how to make them? Thanks. -Gogson
[url]http://wiki.garrysmod.com/page/GM/PlayerSay[/url]
[QUOTE=HappyGhetto;49619047][url]http://wiki.garrysmod.com/page/GM/PlayerSay[/url][/QUOTE] Do I need to make a file or something to put this in?
[QUOTE=Gogson8;49619070]Do I need to make a file or something to put this in?[/QUOTE] Yes?
[QUOTE=HappyGhetto;49619081]Yes?[/QUOTE] A folder, I meant, and where do I put it?
autorun/server ????
I'm afraid of how you made the derma if you don't even know how to use a hook... :speechless::nope:
hook.Add( "PlayerSay", "Example", function( ply, txt ) if txt == "/test" then ply:ChatPrint("Hello!") end end)
If the chat command is just going to open a derma frame you should use [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/GM/OnPlayerChat]GM/OnPlayerChat[/url] instead of playersay
[CODE] local words = { "!hi", "/hi" } local function ChatCommand( ply, text ) if table.HasValue( words, text ) then ply:ConCommand( "open_derma" ) return "" end end [/CODE]
[QUOTE=xbeastguyx;49622025][CODE] local words = { "!hi", "/hi" } local function ChatCommand( ply, text ) if table.HasValue( words, text ) then ply:ConCommand( "open_derma" ) return "" end end [/CODE][/QUOTE] This is fine as the table only has a few entries, though it's better practice to use a lookup table here, [code] local chat_commands = { ["!hi"] = true, ["/hi"] = true, } hook.Add("PlayerSay", "chatcommands", function(ply, text) if chat_commands[string.lower(text)] then -- CODE end end) [/code]
[QUOTE=bigdogmat;49622419]This is fine as the table only has a few entries, though it's better practice to use a lookup table here, [code] local chat_commands = { ["!hi"] = true, ["/hi"] = true, } hook.Add("PlayerSay", "chatcommands", function(ply, text) if chat_commands[string.lower(text)] then -- CODE end end) [/code][/QUOTE] Or you could simply use patterns and avoid indexing or any extra processing/complications: [lua] hook.Add("PlayerSay", "chatcommands", function(ply, text) if text:lower ():match '^[/!]hi$$$' then -- CODE end end) [/lua]
[QUOTE=GGG KILLER;49622528]Or you could simply use patterns and avoid indexing or any extra processing/complications: hook.Add("PlayerSay", "chatcommands", function(ply, text) if text:lower ():match '[/!]hi' then -- CODE endend) [/QUOTE] I was giving a solution for adding more commands onto the example given, though yes you're correct for handling different prefixes that way. EDIT: Though you should anchor the pattern down so it doesn't match that within an entire sentence.
[QUOTE=bigdogmat;49622636]I was giving a solution for adding more commands onto the example given, though yes you're correct for handling different prefixes that way. EDIT: Though you should anchor the pattern down so it doesn't match that within an entire sentence.[/QUOTE] Thanks, edited my solution.
[QUOTE=GGG KILLER;49622664]Thanks, edited my solution.[/QUOTE] You got the anchors mismatched :V
[QUOTE=bigdogmat;49622683]You got the anchors mismatched :V[/QUOTE] Lol, trying to fix them but I think I found an error on fp's highlighter EDIT: Yep, the "$" at the end won't show up no matter how much I edit.... EDIT: Success, at last! had to put 3 $'s but it worked....
Sorry, you need to Log In to post a reply to this thread.