Weird behaviour when creating vehicles on server

Hello, we are currently trying to develop a script for a valet job, where an NPC spawns at a random time, comes to the player and he then parks it into a garage, however, we have just encountered a weird error/behaviour with creating vehicles on server. Both CreateVehicleServerSetter and CreateVehicle.

We are trying to spawn a vehicle, then spawn a ped inside, but with no luck, only the vehicle spawns, the ped doesn’t.

This is the code in question:

AddEventHandler("myscript:server:NewCustomer", function(RandomCar, RandomPed)
    local model = type(RandomCar) == "string" and GetHashKey(RandomCar) or RandomCar
    local veh = CreateVehicleServerSetter(RandomCar, "automobile", Config.CarSpawn, Config.CarSpawn.w or 0.0)
    while not DoesEntityExist(veh) do Wait(0) end
    local netId = NetworkGetNetworkIdFromEntity(veh)

    local ped = CreatePedInsideVehicle(veh, 2, RandomPed, -1, true, true)
    while not DoesEntityExist(ped) do Wait(0) end
    local pedNetId = NetworkGetNetworkIdFromEntity(ped)

    TriggerClientEvent("myscript:client:NewCustomer", -1, netId, pedNetId)
end)

That code is being invoked with this command currently because of debug purposes:

RegisterCommand("newcustomer", function(source)
    local RandomCar = "adder"
    local RandomPed = "S_M_Y_Valet_01"

    TriggerEvent("myscript:server:NewCustomer", RandomCar, RandomPed)
end, false)

The server event then calls the client, which is supposed to handle all the moving and stuff, but it isn’t even called:

RegisterNetEvent("myscript:client:NewCustomer", function(netId, pedNetId)
    -- Configure car
    local veh = NetToVeh(netId)
    SetEntityHeading(veh, Config.CarSpawn.w)
    SetVehicleFuelLevel(veh, 100.0)
    SetVehicleEngineOn(veh, true, true)

    -- Configure ped
    local ped = NetToPed(pedNetId)
    SetPedRandomComponentVariation(ped)
    SetPedRandomProps(ped)
    SetPedFleeAttributes(ped, 0, 0)
    SetPedDiesWhenInjured(ped, false)
    SetBlockingOfNonTemporaryEvents(ped, true)
    SetEntityInvincible(ped, true)
    SetPedCanBeDraggedOut(ped, false)
    ...and continues

The weird behaviour is with the creation on server, when the netId of the spawned vehicle is always 65534 and nothing else, when I tried creating the ped on client, the script errored with this message:

[   4003797] [b2699_GTAProce]             MainThrd/ ^3Warning: [entity] GetNetworkObject: no object by ID 65534^7
[   4003797] [b2699_GTAProce]             MainThrd/ SET_VEHICLE_FUEL_LEVEL: No such entity
[   4003797] [b2699_GTAProce]             MainThrd/ 
[   4003797] [b2699_GTAProce]             MainThrd/ 
[   4004813] [b2699_GTAProce]             MainThrd/ No entity with id '0' exists.

This seems oddly similar to

The reason why the client script is not being run is because the code gets stuck in an infinite loop at while not DoesEntityExist(ped) do Wait(0) end

local ped = CreatePedInsideVehicle(veh, 2, RandomPed, -1, true, true) fails to create the ped (probably because the vehicle Exists on the server side, but not on the client-side, so the RPC native fails to spawn the ped)

It’s easier to just create a normal ped and then put him inside the vehicle. Chances are it will be fast enough that the players won’t notice. Try this:

AddEventHandler("myscript:server:NewCustomer", function(RandomCar, RandomPed)
    local model = type(RandomCar) == "string" and GetHashKey(RandomCar) or RandomCar
    local veh = CreateVehicleServerSetter(RandomCar, "automobile", Config.CarSpawn, 0.0)
    while not DoesEntityExist(veh) do 
        print('waiting for car')
        Wait(0) end
    local netId = NetworkGetNetworkIdFromEntity(veh)
    

    local ped = CreatePed(2, RandomPed, Config.CarSpawn.x,Config.CarSpawn.y,Config.CarSpawn.z+2, 0.0, true, true)
    --local ped = CreatePedInsideVehicle(veh, 2, RandomPed, 0, true, true)
    while not DoesEntityExist(ped) do 
        print('waiting for ped')
        Wait(0) end
    SetPedIntoVehicle(ped, veh, -1)
    local pedNetId = NetworkGetNetworkIdFromEntity(ped)

    TriggerClientEvent("myscript:client:NewCustomer", -1, netId, pedNetId) --Side-note. Sending this to everyone might not be the best idea. Send it to the network owner(s) instead.
end)

Hey, thanks for the response, I tried your solution and this is what I’m getting printed into my client console:
FiveM_b2699_GTAProcess - 11-06-2023 14-51-29

There is absolutely nothing in my server console.
Both the car and ped are created

I modified the last line to be

TriggerClientEvent("myscript:client:NewCustomer", NetworkGetEntityOwner(veh), netId, pedNetId)

as you said in the comment, though when I print NetworkGetEntityOwner or NetworkGetFirstEntityOwner for both the veh and ped, both print as -1

on client-side, make sure to wait for the entities to spawn locally:

while not DoesEntityExist(NetToVeh(netId)) do
    print('waiting for veh')
    Wait(10)
end
local veh = NetToVeh(netId)

while not DoesEntityExist(NetToPed(pedNetId)) do
    print('waiting for ped')
    Wait(10)
end
local ped = NetToPed(pedNetId)

Thanks for the solution, it works now. However, is there a way to make the ped go away when the player takes the keys from him? I am sending the ped object (I guess the client entityId) from ox-target and doing SetPedAsNoLongerNeeded, however it doesn’t seem to be doing anything (I guess because the ped was created on server?)

Try TaskVehicleDriveWander - FiveM Natives @ Cfx.re Docs (for random driving) or TaskVehicleDriveToCoordLongrange - FiveM Natives @ Cfx.re Docs (for driving to specific coords)

Tasks are (mostly) client-side only, and need to be set by the network owner of that entity.

I tried TaskWanderStandard and it seems to work, thanks for everything :slight_smile:

1 Like