[help] Create Vehicle Server

I need help to create vehicle in the server.

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.

client.lua

ESX.TriggerServerCallback('esx_advancedgarage:Crear_Auto', function(spawned_car)
			local auto = NetworkGetEntityFromNetworkId(spawned_car)
			SetVehicleNumberPlateText(auto, plate)
			ESX.Game.SetVehicleProperties(auto, vehicle)
			SetVehRadioStation(auto, "OFF")
			exports["LegacyFuel"]:SetFuel(auto, fuelLevel)
			SetPedIntoVehicle(GetPlayerPed(-1), auto, -1)
			table.insert(autos, { model = vehicle.model, plate = plate, vehicle = auto})
	end, vehicle.model, this_Garage.Spawner, this_Garage.Heading)
	
	TriggerServerEvent('esx_advancedgarage:setVehicleState', plate, false)

Server.lua

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)

Have you tried waiting after the CB for the entity to be created in the client?

local auto = NetworkGetEntityFromNetworkId(spawned_car)
while not DoesEntityExist(auto) then Wait(0) end
...

I just tried it, but the same thing happens, the car comes out without the properties.

This comes out on the console when the car comes out without properties.

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.

I have to try it, but how do you call the SpawnVehicle function from the client?

I always get on top of the car, but the tuning of the vehicles sometimes come out yes and sometimes no.

1 Like

Client Spawn:

TriggerServerEvent('esx_advancedgarage:CrearAuto', vehicle, this_Garage.Spawner, this_Garage.Heading, plate, fuelLevel)

Server Create:

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)
1 Like

this is a fix ?

1 Like