RegisterCommand - Wishing to learn parameters

Hello folks,

I’m new here - I’ve developed my fair share of GTA San Andreas (SAMP) servers in the past and very confident with it - However LUA? That’s a whole new breed of scripting language I now wish to learn.

So to kickstart my understanding of it. I’ve been playing around with some of the functions, mainly ‘RegisterCommand’.

I originally made a command which was along the lines of

RegisterCommand("spawnveh", function(source, args, raw)

	local hash = GetHashKey("zentorno")
	RequestModel(hash)

	while not HasModelLoaded(hash) do
		RequestModel(hash)
		Citizen.Wait(0)
	end

	local x, y, z = table.unpack(GetEntityCoords(GetPlayerPed(-1), false))
	local vehicle = CreateVehicle(hash, x + 2, y + 2, z + 1, 0.0, true, false)
	TriggerEvent("chatMessage", "", { 3, 3, 3 }, "Vehicle Spawned!")
end, false)

Simple enough - Spawns in a Zentorno and sends a message in chat. I’m now wishing to try make it so you can type /spawnveh ‘Zentorno’ or /spawnveh ‘Calvacade’ etc.

So - I always attempt before asking - Let me detail what I did

RegisterCommand("spawnveh", function(source, args, raw)

	local hash = GetHashKey(args)
	RequestModel(hash)

	while not HasModelLoaded(hash) do
		RequestModel(hash)
		Citizen.Wait(0)
	end

	local x, y, z = table.unpack(GetEntityCoords(GetPlayerPed(-1), false))
	local vehicle = CreateVehicle(hash, x + 2, y + 2, z + 1, 0.0, true, false)
	TriggerEvent("chatMessage", "", { 3, 3, 3 }, "Vehicle Spawned!")
end, false)

Now I read the RegisterCommand wiki and realised that ‘source’ is the ID typically of the player typing the command, args is the additional arguments they type (in table format, I believe? This might be my problem) and raw which is the entire command and arguments in a string format.

Now how is it I can extract the parameters from args as it’s evident probably to you professionals that local hash = GetHashKey(args) isn’t going to return anything if it cannot understand what’s been typed. I’m yet to test this but would tostring(args) work?

Thanks, first day here so I hope I’ve explained that relatively well.

RegisterCommand("spawnveh", function(source, args, raw)
    if args[1] == nil then
        print("No model name was supplied.")
        return
    end

    local hash = GetHashKey(arg[1])

    if not IsValidModel(hash) then
        print("Model name supplied is not a valid model.")
        return
    end

	RequestModel(hash)

	while not HasModelLoaded(hash) do
		Citizen.Wait(20)
	end

	local x, y, z = table.unpack(GetEntityCoords(GetPlayerPed(-1), false))
	local vehicle = CreateVehicle(hash, x + 2, y + 2, z + 1, 0.0, true, false)
	TriggerEvent("chatMessage", "", { 3, 3, 3 }, "Vehicle Spawned!")
end, false)

RegisterCommand will turn any additional arguments into an table, you can acces them via an index.

We just need to make sure:

  • Required arguments were passed.
  • The argument passed is a valid model hash

Up to you to handle the cases where the argument wasn’t passed or was an invalid model.

Note: You only have to RequestModel once.

Removed... His is better explained

… Not really sure why it pasted like that… But eh

Also we must have posted at the same time because I was unaware that @Syntasu already answered XD.

I attempted these changes but to no luck, Syntasu.

Getting no response when the argument/parameter is entered. I wasn’t far from getting this so I’m happy I’ve got a better understanding than what I once thought I had.

Does it print into the console?

No the print isn’t even sending it to console now that you mention it. I tried just /spawnveh without the parameter expecting it to return No model name was supplied. Nothing was shown.

Are you sure there isn’t some error? I’ve not tested this code, written it from the top of my head.

1 Like

An image of me connecting to the server, I typed the command and nothing.
http://www.bullet-gaming.com/captures/FC73Cp.png

For the interest of testing purposes, I changed the ‘print’ to a TriggerEvent chatMessage and it did work but not under print it isn’t. Regardless of that - Once the parameter was entered, it still did not spawn the vehicle.

Maybe returning in a RegisterCommand was not a good idea, let me get back to you when i’ve tested it on my server.

I think you can only return though, really. Prevent them from continuing despite not meeting the argument.

Appreciate you doing that @Syntasu


Update:
@Syntasu I managed to get it working removing this part:

    if not IsValidModel(hash) then
       TriggerEvent("chatMessage", "", {3, 3, 3}, "Model name supplied is not a valid model.")
       return
    end

I obviously changed them to chatMessage’s over printing to the console and now it works. However it doesn’t now check to see if it’s a valid model before attempting to spawn it for obvious reasons. This ‘IsValidModel’, I can’t find anything about it on the wiki. Any ideas?

Fixed a few typo’s and for some reason it was using some weird quotes:

RegisterCommand("spawnveh", function(source, args, raw)
	if args[1] == nil then
		TriggerEvent("chatMessage", "^3Spawn-o-mat", {0, 0, 0}, "^1You need to supply a car model!")
		return
	end
	
	local hash = GetHashKey(args[1])
	
	if not IsModelAVehicle(hash) then
		TriggerEvent("chatMessage", "^3Spawn-o-mat", {0, 0, 0}, "^1" .. args[1] .. " is not a valid car model (hash is " .. hash .. ")")
		return
	end
	
	RequestModel(hash)
	
	while not HasModelLoaded(hash) do
		Citizen.Wait(20)
	end
	
	local x, y, z = table.unpack(GetEntityCoords(GetPlayerPed(-1), false))
	local vehicle = CreateVehicle(hash, x + 2, y + 2, z + 1, 0.0, true, false)
	TriggerEvent("chatMessage", "^3Spawn-o-mat", {0, 0, 0}, "^2Spawned a " .. GetDisplayNameFromVehicleModel(hash) .. " for you!")
end, false)
1 Like

“^3Spawn-o-mat”?

What does this function do? What’s it useful for :slight_smile: ?

I’m yet to test this but will try in a second and confirm

its just a chat msg the name of the msg will be Spawn-o-mat

Ah right - Thanks @ExoticNx.

I’ve marked @Syntasu response as solved. It was a little further fetched than what I thought it would be but I can safely say after testing I understand what this all means.

Minus the ^3 before Spawn-o-mat. If anyone could clear that up, that’d be amazing.

image pic frem this post here

its the [YOUR ANNOUNCEMENT] part of the chat msg

and the ^3 is the color

1 Like

Thanks @ExoticNx! You’ve been greatly helpful to me this evening! This and my other post.

1 Like

Anyone know how to make argument become a number type? Cause, I am going to pass a number for me to locate the value of a table. But it keep showing me string value make my codes can’t work properly

You can cast your variable to a number using

tonumber(variable)

You can also just register the command to require a number . He is an example of a command that takes in a few arguments, each of a certain type - amount as a number.

ESX.RegisterCommand('setaccountmoney', 'admin', function(xPlayer, args, showError)

    //do something
end, true, {help = _U('command_setaccountmoney'), validate = true, arguments = {

    {name = 'playerId', help = _U('commandgeneric_playerid'), type = 'player'},

    {name = 'account', help = _U('command_giveaccountmoney_account'), type = 'string'},

    {name = 'amount', help = _U('command_setaccountmoney_amount'), type = 'number'}

}})