How To Make Chat Commands?

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

Do I need to make a file or something to put this in?

Yes?

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

GM/OnPlayerChat instead of playersay



local words = { "!hi", "/hi" }
local function ChatCommand( ply, text )
    if table.HasValue( words, text ) then 
        ply:ConCommand( "open_derma" )
        return ""
    end
end


This is fine as the table only has a few entries, though it’s better practice to use a lookup table here,



local chat_commands = {
  ["!hi"] = true,
  ["/hi"] = true,
}

hook.Add("PlayerSay", "chatcommands", function(ply, text)

  if chat_commands[string.lower(text)] then
    -- CODE
  end

end)


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]

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.

Thanks, edited my solution.

You got the anchors mismatched :V

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…