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.
What are console commands?
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
How do I add them?
You can add console commands anywhere in your code, 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:
concommand.Add( "commandname", function( ply, cmd, args, str ) print( ply:Nick(), cmd ) PrintTable( args ) end )
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 a NullEntity() if it was run from the server.
- 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.
How do I remove them?
You can remove console commands using this code snippet anywhere in your code:
concommand.Remove( "commandname" )
Note, that some ( or all? ) build-in commands are impossible to remove.