[HELP] How to check if Vehicle/PED/etc exists

Hey there,

I am relatively new to FiveM modding. I was checking through http://www.dev-c.com/nativedb/ns/PLAYER to see which function calls are possible. There I have spotted functions like:

Vehicle GET_PLAYERS_LAST_VEHICLE() // B6997A7EB3F5C8C0 E2757AC1

Surely, vehicle must be able to have some null value (probably 0?) just in case the player never entered a vehicle. How do I then for this? I guess:

if vehicle then
   --...
end

Several Questions:

  1. Is this assumption correct?
  2. If so, does this assumption also include PEDs? If not, does the correct way also include PEDs?
  3. How do I check if a vehicle, that might once have been valid, is still present in the game world?
  4. If I get a correct vehicle returned, which is then destroyed, can I still refer to this vehicle via the previous vehicle value?

Thanks in advance!

Got the Answers to my own questions.

  1. No. It seems like there isn’t even a null value. You have to instead use DoesEntityExist which takes a single entity as argument.
  2. See above. This seems to work for anything.
  3. See 1
  4. Dunno yet if a destroyed vehicle is still the same entity.

Not sure about GetPlayersLastVehicle not having a null value… Because GetVehiclePedIsIn returns 0 I believe and a while ago I believe I tried using GetPlayersLastVehicle and it gave me the same result… Not 100% sure though, would have to test it again to be sure. As for the if vehicle then you could just say if vehicle > 0 then that way 0 or -1 will both be considered “null/nil” and you will only trigger the code when you’ve got a valid vehicle.

local playerPed = GetPlayerPed(-1)
local vehicle = GetVehiclePedIsIn(playerPed, false)

if vehicle ~= nil then
    print("ped is in a vehicle")
end

That’s the question ?

1 Like