Vehicles: spawn, modify, clean up
Vehicles are the most common entity you will work with in FiveM. Garage scripts, dealerships, police cruisers, test commands -- they all start with spawning a car and putting the player in it. This lesson covers the full cycle: stream the model in, spawn the car, customize it, put the player inside, and delete it cleanly when the timer runs out.
Build it
Make the resource folder
Inside your server's resources folder, create this folder:
resources/qu_vehicles_spawn_modify_store
Create the files
Create this exact file layout:
resources/qu_vehicles_spawn_modify_store/
fxmanifest.lua
client.lua
Write fxmanifest.lua
Open fxmanifest.lua and paste this:
fx_version 'cerulean'
game 'gta5'
client_script 'client.lua'
Spawning a car and warping the player both happen on the client, so this resource has a client_script and no server_script. Older tutorials add a lua54 'yes' line here. As of June 2025 that setting is deprecated and ignored: Lua 5.4 is now the only Lua runtime, so you leave it out.
Write the lesson code
Open client.lua and paste this. The lines are spread out on purpose so the next section can walk through them one at a time:
local testVehicle
local function loadModel(model)
if not IsModelInCdimage(model) or not IsModelAVehicle(model) then
return false
end
RequestModel(model)
local deadline = GetGameTimer() + 5000
while not HasModelLoaded(model) do
if GetGameTimer() > deadline then return false end
Wait(0)
end
return true
end
RegisterCommand('testvehicle', function()
if testVehicle and DoesEntityExist(testVehicle) then
DeleteEntity(testVehicle)
end
local model = joaat('sultan')
if not loadModel(model) then
print('[qu_vehicles_spawn_modify_store] invalid vehicle model or load timeout')
return
end
local ped = PlayerPedId()
local c = GetEntityCoords(ped)
testVehicle = CreateVehicle(model, c.x + 3.0, c.y, c.z, GetEntityHeading(ped), true, false)
SetVehicleNumberPlateText(testVehicle, 'QUCAR')
SetVehicleColours(testVehicle, 12, 12)
TaskWarpPedIntoVehicle(ped, testVehicle, -1)
SetModelAsNoLongerNeeded(model)
print('[qu_vehicles_spawn_modify_store] spawned sultan')
local spawned = testVehicle
SetTimeout(60000, function()
if spawned and DoesEntityExist(spawned) then
DeleteEntity(spawned)
print('[qu_vehicles_spawn_modify_store] auto-deleted')
end
end)
end, false)
Start and test it
Open server.cfg and add this line:
ensure qu_vehicles_spawn_modify_store
Save, then run this in the server console:
restart qu_vehicles_spawn_modify_store
Join the server, open the F8 console, and run:
/testvehicle
A Sultan appears beside you, you are dropped into the driver seat, the plate reads QUCAR, and the body is matte black. Sixty seconds later the car vanishes.
Keep reading the full lesson
Sign in to start, then unlock every step of this lesson and the full FiveM School with a membership.
- How it works
- If something went wrong
- What you can do now
- Try it yourself
The remainder of Vehicles: spawn, modify, store is available to FiveM School members.