From GMod Wiki
|Description:||Article about concommands.|
|Created:||October 24th 2010|
This is an article which will explain you the basics of using console command functions in lua.
You will probably be using this one a lot, it adds a console command into your console when used client-sided, or to everyone's consoles when used server-sided. The first argument is what ever you want to have as the console command, and the second argument is the function you want to run with it. There are two different ways to use this, this is the first one.
concommand.Add( "ConsoleCommand", SomeFunction )
This is the second one, which I personally prefer to use.
concommand.Add( "ConsoleCommand", function() end )
You can try its' functionality by just running this simple script. Just type
ConsoleCommand in console to test it, after you have loaded the script.
concommand.Add( "ConsoleCommand", function() print( "I am your first console command!" ) end )
Console command with arguments
You'll want propably pretty often to have arguments on your console commands, I'll show you how it's made. The first argument returns the player who ran the console command, second returns what console command he ran, the third returns any possible arguments.
concommand.Add( "ConsoleCommand", function( ply, cmd, args ) end )
Lets make a simple code to test out arguments.
// ply = Player // cmd = Console command // args = Arguments concommand.Add( "ConsoleCommand", function( ply, cmd, args ) if args then // We're seeing if there are any arguments, arguments are stored on a table, so you will have to use args[index]. print( ply:Name() .. " ran the console command " .. cmd .. " with the argument " .. args ) else print( ply:Name() .. " ran the console command " .. cmd .. " with no arguments." ) end end )
More info about this on concommand.Add page.
Sometimes, you might want to remove a console command. It's going to be very simple, since there is only one argument in the function and that would be the console command.
concommand.Remove( "ConsoleCommand" )
More info about this on the concommand.Remove page.
This is a short article, but I hope you learned something from this.