Client and server in depth · Lesson 4/4 · 9 min

Networked entities: server-side vehicles

Creating vehicles from the server so everyone sees them the same and without cheating.

If you create a vehicle only on the client, others don't see it or see it wrong. The right approach in modern FiveM (with or without QBCore) is to create entities on the SERVER; that way they're synced for everyone and harder to tamper with.

lua
-- SERVER: creates the vehicle and returns its netId
RegisterNetEvent('garaje:server:spawn', function(model, coords)
  local src = source
  -- validate that the player can (job, ownership, distance)...
  local veh = CreateVehicle(model, coords.x, coords.y, coords.z, coords.w, true, true)
  while not DoesEntityExist(veh) do Wait(0) end
  local netId = NetworkGetNetworkIdFromEntity(veh)
  TriggerClientEvent('garaje:client:setOwner', src, netId)
end)

server.lua

lua
-- CLIENT: from the netId to the local entity to get inside
RegisterNetEvent('garaje:client:setOwner', function(netId)
  local veh = NetToVeh(netId)
  local tries = 0
  while not DoesEntityExist(veh) and tries < 100 do
    veh = NetToVeh(netId); tries = tries + 1; Wait(10)
  end
  if DoesEntityExist(veh) then
    TaskWarpPedIntoVehicle(PlayerPedId(), veh, -1)
  end
end)

client.lua

netId (network ID) is an entity's "ID card" that's the same on every client. Server creates → sends netId → each client resolves it to its local entity with NetToVeh/NetToObj/NetToPed. In QBCore this is common in garages (qb-garages follows this pattern).

Practice what you learned

0/3
Test

¿Por qué se crean los vehículos en el SERVIDOR y no solo en el cliente?

Rellena los huecos

Completa la creación server-side del vehículo y la espera a que exista antes de obtener su netId.

1local veh = CreateVehicle(model, coords.x, coords.y, coords.z, coords.w, true, true)
2while not (veh) do Wait(0) end
3local netId = NetworkGetNetworkIdFromEntity(veh)
Pista

Tras CreateVehicle la entidad tarda un instante en existir; espera en un bucle corto hasta que la comprobación devuelva true.

Ordena el código

Ordena el evento de servidor que crea un vehículo, espera a que exista y manda su netId al cliente dueño.

Coloca las líneas en el orden correcto con las flechas.

local veh = CreateVehicle(model, coords.x, coords.y, coords.z, coords.w, true, true)
RegisterNetEvent('garaje:server:spawn', function(model, coords)
TriggerClientEvent('garaje:client:setOwner', src, netId)
end)
while not DoesEntityExist(veh) do Wait(0) end
local netId = NetworkGetNetworkIdFromEntity(veh)
local src = source
Pista

source → crear vehículo → esperar a que exista → sacar netId → avisar al cliente con ese netId.

Challenge: code it yourself

Make it so that when the vehicle is spawned on the server, it also gets a specific license plate before notifying the client.

Write it yourself in your editor (VS Code) and test it on your server. You learn here by doing it, not by copying.

See hint

After CreateVehicle and DoesEntityExist, call SetVehicleNumberPlateText(veh, 'CRX 001').

Escribe aquí tu solución:

How was this lesson?