How do I collect arguments from commands (ex… /spawn [vehiclename] )
I want to make a simple command that spawns the vehicle that you have chosen, for example: (/spawn zentoro) would collect zentoro as an argument and then I would be able to use it elsewhere.
I think a few resources like this already exist though I know one was built into essentialmodes es_admin and partially removed in es_admin2
(If you use essentialmode here is the code for the one I use)
Client Script
-- Called by server.lua for Spawning vehicles
RegisterNetEvent('es_com:spawnVehicle')
AddEventHandler('es_com:spawnVehicle', function(v)
local carid = GetHashKey(v)
local playerPed = GetPlayerPed(-1)
if playerPed and playerPed ~= -1 then
RequestModel(carid)
while not HasModelLoaded(carid) do
Citizen.Wait(0)
end
local playerCoords = GetEntityCoords(playerPed)
veh = CreateVehicle(carid, playerCoords, 0.0, true, false)
SetVehicleAsNoLongerNeeded(veh)
TaskWarpPedIntoVehicle(playerPed, veh, -1)
end
end)
Server Script
-- modified to work from es_admin
TriggerEvent('es:addCommand', 'car', function(source, args, user)
TriggerClientEvent('es_com:spawnVehicle', source, args[1])
-- sometimes stops working (maybe to do with another script)
-- for some reason fixes by changing args
-- could be windows/linux differences for fxserver
end)
you could modify this bit to work without essentialmode
for example
-- Add an event handler for the 'chatMessage' event
AddEventHandler( 'chatMessage', function(source, args, user)
msg = string.lower( msg )
-- Check to see if a client typed in /dv
-- originally based off of delete vehicle
if ( msg == "/spawn" or msg == "/car" ) then
-- Cancel the chat message event (stop the server from posting the message)
CancelEvent()
-- Trigger the client event
TriggerClientEvent( 'es_com:spawnVehicle', source,args[1] )
end
end )
edit: ^ Ive never tested this last bit of code but you can always tinker and correct.
No offense but there’s a way easier solution
Here’s a simple /car <vehicleName> example, to get the arguments, just use args[n] where n is the nth argument (starting at 1). If for example you want to get the 4th argument, but the user hasn’t provided a 4th argument, getting args[4] will return nil. The script below spawns the car in front of you. If no car is specified it spawns the adder by default.
RegisterCommand('car', function(source, args, rawCommand)
local x,y,z = table.unpack(GetOffsetFromEntityInWorldCoords(PlayerPedId(), 0.0, 8.0, 0.5))
local veh = args[1]
if veh == nil then veh = "adder" end
vehiclehash = GetHashKey(veh)
RequestModel(vehiclehash)
Citizen.CreateThread(function()
local waiting = 0
while not HasModelLoaded(vehiclehash) do
waiting = waiting + 100
Citizen.Wait(100)
if waiting > 5000 then
TriggerEvent('chatMessage', '', {255,255,255}, '^8Error: ^1Took too long to load the vehicle model. Are you sure it\'s a valid model?')
break
end
end
CreateVehicle(vehiclehash, x, y, z, GetEntityHeading(PlayerPedId())+90, 1, 0)
end)
end)
RegisterCommand('car', function(source, args) -- unused args aren't needed
local pos = GetOffsetFromEntityInWorldCoords(PlayerPedId(), 0.0, 8.0, 0.5) -- native vector3s ftw?
local veh = args[1]
if veh == nil then veh = "adder" end
RequestModel(veh) -- no need to hash for any arguments that are documented as Hash
-- refs automatically run in a CreateThreadNow, I believe
local waiting = 0
-- not really a good idea, invalid models can be checked with IsModelInCdimage
-- and long loads will still hang in the streamer
while not HasModelLoaded(veh) do
waiting = waiting + 100
Citizen.Wait(100)
if waiting > 5000 then
TriggerEvent('chatMessage', '', {255,255,255}, '^8Error: ^1Took too long to load the vehicle model. Are you sure it\'s a valid model?')
break
end
end
-- yes, this kind of violates the function definition, but as Lua is dynamically typed it doesn't matter
-- you could also just use pos.x, pos.y, pos.z if you want to be pedantic
local cveh = CreateVehicle(veh, pos, GetEntityHeading(PlayerPedId())+90, true, false)
-- to allow the game to manage the vehicle's lifetime
SetEntityAsNoLongerNeeded(cveh)
-- so that it won't actually keep the vehicle model loaded
SetModelAsNoLongerNeeded(veh)
end)
I’ll test it later but a few weeks ago I tried spawning a default gta v (non dlc) vehicle and it just wouldn’t load. Others would so it was only that one.
My bad, tried it again and you are right. It thought it worked, but maybe I had a faulty if at some point (asked if the model was valid) and it still tried to spawn it.
Just tried it again, the car was schafter2 it used to never spawn, now it takes about 5-8 seconds for it to load the first time using lambda menu but after that it’s instant. Must’ve been something wrong with the other trainer then. Good to know it works though
yes sir I saw this, but the problem with this is that it uses a “stringsplit” function to collect the arguments… i’m trying to just collect them right from the original command if you know what im trying to say
RegisterCommand('commandName', function(source, args rawCommand)
local argument1 = args[1] -- (will be equal to nil if argument 1 doesn't exist)
local argument2 = args[2]
--- etc...etc...
end, false) -- set this to true if you want the command to be restricted.