Console commands are easy to add and remove. This is actually one of the easiest things you can learn in Lua and yet it could mean a lot to your actual coding process and time you take up to code whatever you are coding.
Console commands can be called from the console of the server or client at any time and the function of the command will be executed. This allows the server owner or client to perform actions upon the server. For example, kicking a player.
Please don't confuse console commands with console variables
You can add console commands anywhere in your code by using the concommand.Add function, just make sure that it is outside of any functions or hooks, so that the command will be added as soon as Lua system initializes.
Here's an example console command:
The first argument is the command name.
The second argument is a function which have 3 arguments:
- Player - can either be a player object or, if called from the server, a NULL entity.
- Command - the command entered.
- Arguments - a table of strings containing the arguments entered. This is what you enter after the command, separated by a space (" ")
- Full string - the whole text that was entered into the console.
You can remove console commands using the concommand.Remove function as so:
Note that only commands added by Lua can be removed.