FiveM vehicles disappearing or despawning: why it happens and the fix

Spawned cars vanish after a few seconds or when you walk away? OneSync entity culling, orphan mode, owners leaving, cleanup scripts and how to keep a vehicle persistent.

Typical symptoms:

text
You spawn a car, walk a few hundred metres, and it is gone. Or a car you left on the street is missing after you log out.

There are several different systems that delete vehicles, and each has a different fix. This article explains each one, and the ways to make a vehicle persistent.

Who deletes a vehicle

A networked vehicle can be removed by:

  1. Entity culling. On OneSync, the server only keeps entities that are relevant to at least one player. An entity far from everyone can be removed.
  2. The owner leaving. An entity has an owner client that simulates it. If the owner disconnects, the entity can be removed unless the server tells the game to keep it.
  3. The game's population cleanup. The game deletes ambient vehicles it considers not needed. A car created in a certain way can be treated as one of them.
  4. A script. Many servers run a cleanup script that deletes unoccupied cars on a timer.
  5. Your own code. A DeleteEntity in a loop, or a script that deletes anything near the player.

Find which one it is before changing code.

Cars vanish at a fixed time

If the vehicle disappears after exactly the same delay, it is a cleanup script. Search your resources for DeleteEntity, DeleteVehicle and GetAllVehicles:

bash
grep -ri "GetAllVehicles" resources/ --include=*.lua

Look for a loop that deletes any vehicle without a driver. Most of them have a config for a whitelist or a delay. Fix it there, not in the vehicle code.

Cars vanish when you walk away

This is culling and the owner rule. OneSync does not keep entities that no one is near. When you spawn a car for a player and the player leaves its area, the car is a candidate for removal.

The server can decide what happens with the entity with its orphan mode:

lua
-- server
SetEntityOrphanMode(vehicle, 2)

SetEntityOrphanMode takes the entity and a mode. The modes are: 0, delete the entity when it is no longer relevant (the default); 1, delete it when its owner disconnects; 2, keep it. Check the current native reference for your server build before relying on the numbers.

The vehicle must be created on the server for this to be reliable. A car created by a client, with CreateVehicle, belongs to that client first. See spawning a vehicle on the server for the full flow:

lua
local vehicle = CreateVehicleServerSetter(joaat('sultan'), 'automobile', x, y, z, heading)

while not DoesEntityExist(vehicle) do
    Wait(0)
end

SetEntityOrphanMode(vehicle, 2)

Warning: a kept entity stays until you delete it. If you set orphan mode to keep on every car, abandoned vehicles pile up and your entity count rises. Delete them yourself when the player stores the car or leaves.

Needs OneSync

Most of this only applies on OneSync. Check that it is enabled in server.cfg, and read how to enable OneSync. Without it, vehicles are client based and disappear with their owner.

The game's cleanup on the client

When you spawn a car on the client, the game may treat it as part of the ambient population and remove it. Mark it as a mission entity so it is treated as yours:

lua
local vehicle = CreateVehicle(model, x, y, z, heading, true, false)
SetEntityAsMissionEntity(vehicle, true, true)

SetEntityAsMissionEntity also matters when you need to delete a vehicle later. DeleteEntity on a vehicle the client does not own as a mission entity can silently fail.

A common pattern is to take control of an entity and then delete it:

lua
local function removeVehicle(vehicle)
    if not DoesEntityExist(vehicle) then return end
    SetEntityAsMissionEntity(vehicle, true, true)
    DeleteEntity(vehicle)
end

Persistent vehicles: the real approaches

A garage does not keep the car in the world. It keeps a row in the database, and spawns the car when needed. For persistent street parking, there are two approaches:

Keep the entity

Create the car on the server, set the orphan mode to keep, and remember it (net id and plate) in a table. The car stays in the world while the server runs. Delete it when its owner stores the car.

This is simple but costs entities, and everything is lost at a restart. It suits a small number of cars.

Save and respawn

Save the position and properties of the parked car in the database, delete the entity, and spawn it again when a player gets near. This scales to many cars, and survives a restart:

  1. When the player parks outside, save the plate, coordinates, heading and properties.
  2. Remove the entity.
  3. When a player enters a radius, or at server start, spawn it again from the row.

The properties are covered in vehicle properties, and the table side in garage tables.

Other causes

The player is a lot of metres away and the car is far from any cull radius. Entities beyond a certain distance are not synced to a client. A car can look missing on a client far from it while still existing on the server. Check on the server with DoesEntityExist(vehicle) before assuming it is deleted.

Routing buckets. A vehicle in another routing bucket than the player is invisible to them. See routing buckets.

Entity limit. A very high number of vehicles hits the engine's limits. Remove unused vehicles instead of keeping everything.

The net id is stale. A script that keeps a net id for a car that was deleted holds a handle that no longer exists. Check with NetworkDoesEntityExistWithNetworkId before using it.

Checklist

Symptom Fix
Car disappears at the same time each time Find the cleanup script and configure or remove it
Car disappears when you walk away Create it server side and set SetEntityOrphanMode to keep
Car disappears when the owner logs out Orphan mode keep, or save and respawn from the database
Client created car gets deleted SetEntityAsMissionEntity(vehicle, true, true)
DeleteEntity does not delete Set it as a mission entity first on the owner client
Cars pile up Delete vehicles when stored, do not keep every entity
Car invisible to one player Check the routing bucket and that OneSync is on

Quick answers

Why do parked cars disappear when I walk away?

On OneSync, an entity that is no longer relevant to any player is removed by the server. If you want a parked car to stay, the server has to keep it, for example by setting its orphan mode, or the script must respawn it when someone returns.

What is SetEntityOrphanMode?

A server native that tells the game what to do with an entity whose owner left. It can delete the entity, delete it when it is no longer relevant, or keep it.

Do I still need SetEntityAsMissionEntity?

On the client it still helps against the game's own population cleanup, which can delete a car it thinks is ambient. It does not replace the server side settings.

Scripts that skip this problem

Advanced BoostingTablet-driven vehicle boosting: contracts from class D to S+, crews and a live queue.View script →Chameleon Paints82 colour-shifting chameleon paints in one click-to-apply menu.View script →Car BombPlant, detect and defuse vehicle bombs — with a detonator phone and a C4 minigame.View script →

Keep reading