Is there a difference between GetClosestVehicle & ESX.Game.GetClosestVehicle?

I’m writing a PDM script and I notice on a decent amount of cars GetClosestVehicle returns nothing, I saw in the documentation that this is a known thing. I was curious if ESX.Game.GetClosestVehicle is any different, and what does it return? The ESX documentation just shows the input.

Just wondering if it’s even worth the effort of ripping up my script to change everything to ESX.Game or if I need to find a different option.

The code of ESX’s getclosestvehicle function can be viewed in the repo

As you can see they pull all the vehicles in the world and then do a distance check to get the closestvehicle

1 Like

Would this be more reliable then FiveM’s GetClosestVehicle? Or does it have the same faults?

I’d use the native one if I were you This relies less on dependencies and should run better.

Well my issue is some vehicles, for example most off road vehicles return 0. I’d have to cut like 30% of the vehicles from PDM because GetClosestVehicle returns 0 so I was looking into other options. I know there is casting a ray in front of a player to get a car, I use that for my repair resource, but it doesn’t really work for this situation. I need to get the car at specific coords even if the player isn’t near it or looking at it reliably.

Try viewing this:

https://runtime.fivem.net/doc/natives/#_0xF73EB622C4F1689B

This native can have some problems with some vehicles, that’s why he might wants to use the esx getclosestvehicle function (or atleast something based on it) (or check the snippet in the native ref for it)

1 Like

After some playing around and debugging I found that it’s actually not GetClosestVehicle but what I was doing with the info that had the effect. Yesterday I was quite tired and just assumed GetClosestVehicle was the culprit because of the comments on the native.

Thanks for the help both of you. :slight_smile: