fivem icon indicating copy to clipboard operation
fivem copied to clipboard

CreateVehicleServerSetter spawn random ped inside

Open CeebDev opened this issue 11 months ago • 1 comments

What happened?

Sometimes (depend on model and coords zone) when creating a vehicle with CreateVehicleServerSetter, there is NPC ped spawning inside

https://github.com/citizenfx/fivem/assets/11323987/b0d1baaa-594d-482e-acc0-b24402950fd1

Expected result

Vehicle should spawn without any NPC ped inside 100% of time

Reproduction steps

  • Go to vec3(-64.7925, -1075.2173, 27.0104)
  • Execute this server command
  • Check logs for how many attemps before a random ped spawn inside
RegisterCommand('createveh', function(source)
    local coords = vector3(-61.5488, -1071.5652, 27.2264)
    local model = "asterope"
    local num = 0
    while true do
        local veh = CreateVehicleServerSetter(model, "automobile", coords.x, coords.y, coords.z, 90.0)
        Wait(100)
        local ped = GetPedInVehicleSeat(veh, -1)
        num = num + 1
        if ped > 0 then break end
        DeleteEntity(veh)
        if num == 100 then
            return print(('failed to spawn vehicle with random ped after %s attempts (try again)'):format(num))
        end
    end
    print(('spawned vehicle with random ped after %s attempts'):format(num))
end)

Ex :

[          script:dev] spawned vehicle with random ped after 1 attempts
[          script:dev] spawned vehicle with random ped after 15 attempts
[          script:dev] spawned vehicle with random ped after 1 attempts
[          script:dev] spawned vehicle with random ped after 1 attempts
[          script:dev] spawned vehicle with random ped after 17 attempts
[          script:dev] spawned vehicle with random ped after 1 attempts
[          script:dev] spawned vehicle with random ped after 1 attempts
[          script:dev] spawned vehicle with random ped after 20 attempts

Importancy

Slight inconvenience

Area(s)

FiveM, OneSync

Specific version(s)

Fx: b7326 / FiveM: 7619

Additional information

No response

CeebDev avatar Mar 17 '24 02:03 CeebDev

I can confirm this and wanted to add to this that the entity sometimes can't be found either after spawning the vehicle so I had to add hacky code like this:

server side:

while not DoesEntityExist(veh) do
    Wait(0)
end
    
while GetVehicleNumberPlateText(veh) == "" do
    Wait(0)
end

client side:

while not NetworkDoesNetworkIdExist(netId) do Wait(10) end
local veh = NetworkGetEntityFromNetworkId(netId)

and all this can take up to 10+ seconds for the vehicle to be available in code so I can apply modifications etc.

It happens more frequently for people with potato computers but it can happen to anyone.

JonasDev17 avatar Mar 18 '24 10:03 JonasDev17