Make sure to double-check your command setup. You will get odd errors because your command arguments are not being parsed correctly. You intend to have a command that takes 2 parameters, but only have a single cmd:addParam instruction.
Start with a tiny piece of working code. Make small changes and verify that they work correctly. This makes it
much easier to pinpoint mistakes! Use
print to display the contents of your arguments in the server console.
1. Let's start with the basic command template without parameters.
local CATEGORY_NAME = "Admin"
function ulx.refund( calling_ply )
print( "Command called by", calling_ply )
end
local refund = ulx.command( CATEGORY_NAME, "ulx refund", ulx.refund, "!refund" )
refund:defaultAccess( ULib.ACCESS_ADMIN )
refund:help( "Refunds a player the amount specified" )
2. Now let's set up the first parameter for the command: a player (not self).
local CATEGORY_NAME = "Admin"
function ulx.refund( calling_ply, target_ply )
print( "Command called by", calling_ply )
print( "We're targeting", target_ply )
end
local refund = ulx.command( CATEGORY_NAME, "ulx refund", ulx.refund, "!refund" )
refund:addParam{ type=ULib.cmds.PlayerArg, target="!^" }
refund:defaultAccess( ULib.ACCESS_ADMIN )
refund:help( "Refunds a player the amount specified" )
3. Now let's set up the second parameter for the command: a number (bigger than 0, integer).
local CATEGORY_NAME = "Admin"
function ulx.refund( calling_ply, target_ply, money )
print( "Command called by", calling_ply )
print( "We're targeting", target_ply )
print( "We're refunding", money )
end
local refund = ulx.command( CATEGORY_NAME, "ulx refund", ulx.refund, "!refund" )
refund:addParam{ type=ULib.cmds.PlayerArg, target="!^" }
refund:addParam{ type=ULib.cmds.NumArg, hint="money", min=1, ULib.cmds.round }
refund:defaultAccess( ULib.ACCESS_ADMIN )
refund:help( "Refunds a player the amount specified" )
4. Now that we've verified our arguments are parsed correctly, we can refund the player. Looks like the correct function to give money to a player is
Player:addMoney.
local CATEGORY_NAME = "Admin"
function ulx.refund( calling_ply, target_ply, money )
target_ply:addMoney( money )
end
local refund = ulx.command( CATEGORY_NAME, "ulx refund", ulx.refund, "!refund" )
refund:addParam{ type=ULib.cmds.PlayerArg, target="!^" }
refund:addParam{ type=ULib.cmds.NumArg, hint="money", min=1, ULib.cmds.round }
refund:defaultAccess( ULib.ACCESS_ADMIN )
refund:help( "Refunds a player the amount specified" )