Alr, so there's 2 ways to do this, both giving the same result. I'm gonna show both of them, then explain them:
Code:
local function on_cmd_command(msg)
djui_chat_message_create("You ran a command, good job! You also gave me this message: " .. msg)
return true
end
hook_chat_command("cmd", "This CMD does something, go find out!", on_cmd_command)
Code:
hook_chat_command("cmd", "This CMD does something, go find out!", function(msg)
djui_chat_message_create("You ran a command, good job! You also gave me this message: " .. msg)
return true
end)
The first way is the way Mariodoesvr was trying to communicate. The function does not need to be
local
, it can be a regular function. The
local
keyword in lua means that it can't be used in other scopes, so if you need to use a function in another file, be sure to remove the
local
keyword. If you see mods making every single function at the top of the file
local
, you don't need to do this. The performance gain is minimally and really isn't necessary, mainly use local's to keep your code organized.
The second way is what you were trying to do. Instead of creating a function somewhere else, and passing it into
hook_chat_command
, you can instead create the function there. A
local
keyword is not needed, nor can it be done, because you aren't creating a variable. Whether or not option 1 or 2 is better depends on your personal preference, and how much code is in your function.
A couple of things that were not mentioned: The function passed to
hook_chat_command
receives a
msg
(message) parameter. It doesn't have to be
msg
, since that's just a variable, name it whatever you want. The message parameter is the message the user typed after the command.
Another thing is in these examples I'm returning
true
. By returning
true
, this tells coop that the command succeeded, and avoids the
Unknown command, type /help for help
text from appearing.
If you have any more questions feel free to ask!