I am currently using this method, but the problem I have is that sometimes they come out with the properties and sometimes not, I need them to always come out with the properties.
ESX.RegisterServerCallback('esx_advancedgarage:Crear_Auto', function(source, cb, car, coords, Heading)
local spawned_car = CreateVehicle(car, coords, Heading, true, true)
SetPedIntoVehicle(playerPed, veh, -1);
while not DoesEntityExist(spawned_car) do Wait(0) end -- or Wait(500)
local netId = NetworkGetNetworkIdFromEntity(spawned_car)
while not netId do Wait(0) end -- or Wait(500)
cb(netId)
end)
I will be the first to say I’m not brilliant when it comes to the LUA side… And I don’t know ESX at all…
Maybe try something like this?
Server:
-- Server side
function SpawnVehicle(hash, coords, heading, callback)
-- Spawn a vehicle in the world
local entity = CreateVehicle(hash, coords, heading, true, true)
-- Wait while the entity is being created
while not DoesEntityExist(entity) do
Wait(0)
end
-- Get a network ID for the entity
local networkId = NetworkGetNetworkIdFromEntity(entity)
-- Trigger the callback
callback(networkId)
end
Client:
-- The client side callback function
function callback(networkId)
-- Wait for the entity to exist on network
while not NetworkDoesEntityExistWithNetworkId(networkId) do
Wait(0)
end
-- Get entity from networkID
local entity = NetworkGetEntityFromNetworkId(networkId)
-- Wait until the entity exists for the client
while not DoesEntityExist(entity) do
Wait(0);
end
-- Make sure that the entity is a vehicle
if not IsEntityAVehicle(entity) then
return
end
-- Car should exist.
-- Finish what you want to do with it, set plate, color etc.
SetVehicleNumberPlateText(entity, "SUCCESS")
SetVehRadioStation(entity, "OFF")
end
Biggest change there is waiting for the NetworkDoesEntityExistWithNetworkId, if this doesn’t do the trick maybe the problem isn’t in the spawning of the vehicle, but in the function that sets the vehicle properties.
RegisterServerEvent('esx_advancedgarage:CrearAuto')
AddEventHandler('esx_advancedgarage:CrearAuto', function(car, coords, Heading, plate, fuelLevel)
local _source = source
local entity = CreateVehicle(car.model, coords, Heading, true, true)
-- Wait while the entity is being created
while not DoesEntityExist(entity) do
Wait(0)
end
-- Get a network ID for the entity
local networkId = NetworkGetNetworkIdFromEntity(entity)
-- Trigger the callback
TriggerClientEvent('esx_advancedgarage:CrearAuto', _source, networkId, car, plate, fuelLevel)
end)
Client add props:
RegisterNetEvent("esx_advancedgarage:CrearAuto")
AddEventHandler("esx_advancedgarage:CrearAuto", function(networkId, vehicle, plate, fuelLevel)
while not NetworkDoesEntityExistWithNetworkId(networkId) do
Wait(0)
end
-- Get entity from networkID
local entity = NetworkGetEntityFromNetworkId(networkId)
-- Wait until the entity exists for the client
while not DoesEntityExist(entity) do
Wait(0);
end
-- Make sure that the entity is a vehicle
if not IsEntityAVehicle(entity) then
return
end
-- Car should exist.
-- Finish what you want to do with it, set plate, color etc.
ESX.Game.SetVehicleProperties(entity, vehicle)
SetVehRadioStation(entity, "OFF")
SetVehicleFixed(entity)
SetVehicleDeformationFixed(entity)
TaskWarpPedIntoVehicle(GetPlayerPed(-1), entity, -1)
exports["LegacyFuel"]:SetFuel(entity, fuelLevel)
TriggerServerEvent('esx_advancedgarage:setVehicleState', plate, false)
end)