PiTyUs.Hire me

Engineering

Server-side entity spawning in FiveM

Spawn entities from the server in FiveM with OneSync: CreateVehicleServerSetter vs CreateVehicle, vehicle types, handing the network ID to the client, putting the player in the seat, plates and state, cleanup, routing buckets and entity lockdown.

Updated 11 min readBy PiTyUs · FiveM developer

Overview

Spawning a garage car on the client is how most scripts start — and how most exploits work, because the client can spawn anything. Moving creation to the server means the server decides what exists, knows the network ID immediately, and lets you turn on entity lockdown so cheaters cannot spawn at all.

01Spawning a vehicle

server.lualua
local function spawnVehicle(src, model, coords, heading, plate)
    local hash = type(model) == 'string' and joaat(model) or model
    local veh = CreateVehicleServerSetter(hash, 'automobile', coords.x, coords.y, coords.z, heading)

    local deadline = GetGameTimer() + 5000
    while not DoesEntityExist(veh) do
        if GetGameTimer() > deadline then return nil end
        Wait(0)
    end

    if plate then SetVehicleNumberPlateText(veh, plate) end
    Entity(veh).state:set('owner', src, true)
    SetPedIntoVehicle(GetPlayerPed(src), veh, -1)

    return veh, NetworkGetNetworkIdFromEntity(veh)
end
Vehicle class`type` value
Cars, vans, trucks, quadsautomobile
Motorbikes, bicyclesbike
Boatsboat
Helicoptersheli
Planesplane
Submarinessubmarine
Trailerstrailer
A wrong type spawns a broken vehicle — match it to the model.

02Finishing on the client

Some things — mods applied from saved properties, fuel scripts, keys — still run on the client. Send the network ID, let the client wait until the vehicle exists locally, then apply them. The pattern is in entity ownership and network IDs.

server.lua → client.lualua
-- server
local veh, netId = spawnVehicle(src, 'sultan', vec3(-45.0, -1098.0, 26.0), 70.0, 'PITY 01')
if netId then TriggerClientEvent('garage:spawned', src, netId, props) end

-- client (waitForNet from the network ID guide)
RegisterNetEvent('garage:spawned', function(netId, props)
    local veh = waitForNet(netId)
    if not veh then return end
    -- apply saved mods here, e.g. lib.setVehicleProperties(veh, props)
end)

Peds and objects

server.lualua
local ped = CreatePed(4, `s_m_y_cop_01`, 441.0, -982.0, 30.7, 90.0, true, true)
local obj = CreateObjectNoOffset(`prop_barrier_work05`, 400.0, -980.0, 29.4, true, true, false)

FreezeEntityPosition(obj, true)

A server-created ped has no behaviour of its own. A handful of task natives exist server-side (TaskPlayAnim, TaskGoStraightToCoord, TaskEnterVehicle, TaskCombatPed…), but richer AI — scenarios, driving styles, relationship groups — is set by the owning client. A common pattern is to create the ped on the server and let the client drive its behaviour.

Cleanup

  • Track what each resource created and DeleteEntity it in onResourceStop.
  • Despawn player vehicles when they are stored or the owner has been gone for a while.
  • Use SetEntityOrphanMode(entity, 2) only for entities you clean up yourself.
server.lualua
local spawned = {}

-- after creating: spawned[#spawned + 1] = veh

AddEventHandler('onResourceStop', function(res)
    if res ~= GetCurrentResourceName() then return end
    for _, e in ipairs(spawned) do
        if DoesEntityExist(e) then DeleteEntity(e) end
    end
end)

Entity lockdown and routing buckets

`sv_entityLockdown`Meaning
inactiveClients can create any entity
relaxedEntities created by client scripts are blocked
strictNo entities can be created by clients at all

The same modes can be set per routing bucket with SetRoutingBucketEntityLockdownMode, and SetEntityRoutingBucket moves an entity into a bucket (an instance). Server-side spawning is the precondition for both.

Frequently asked questions

How do I spawn a vehicle on the server in FiveM?

Use CreateVehicleServerSetter(model, 'automobile', x, y, z, heading), wait for DoesEntityExist, then send its network ID to the client if needed.

What is the difference between CreateVehicle and CreateVehicleServerSetter?

Server CreateVehicle relies on an entity creation RPC; CreateVehicleServerSetter creates it with server setter logic and is documented as the more reliable option.

Do I need RequestModel on the server?

No. RequestModel is a client native. The model hash must still be valid and streamed.

Why does my server-spawned ped not move?

It has no tasks yet. Use one of the server task natives such as TaskGoStraightToCoord, or let the owning client give it its behaviour.

Need this built, not just explained?

Ten years of FiveM work, from Lua to NUI

Custom resources, React NUI, ESX / QBCore / Qbox integration, OneSync performance audits and security reviews — plus the websites and SEO around your server brand.

Related guides