GetClosestVehicle Doesn't work when vehicle is moving - Any Alternatives?

Hey guys!

I’m working on another script, and am really, really stuck:
I’m currently trying to get a targetplane of a specific type, and I’ve found GetClosestVehicle to work marvelous. Here’s why:

  1. It returns ONLY 1 specific vehicle
  2. It gets the closest vehicle

In my scripting endeavors, I’ve unfortunately discovered that if the vehicle is moving, (this may be due to the script it belongs to), the native no longer works. The vehicleID is initially the output of the command…until said vehicle starts moving…

My problem is that there’s no accurate alternative for this native in which I can think of.

I’ve considered using raycasting capsule (StartShapeTestCapsule), however a problem occurs if two or more vehicles of that type are close to the player. I can’t believe I’m stuck asking this, but:

How can I get a SINGLE SPECIFIC vehicle, closest to the player that’s moving?

Any ideas?

Maybe I don’t know wtf I’m talking about; I just tested this in the middle of the road with random vehicles that pass me by using the following code:

Citizen.CreateThread(function()
        while true do
                player = GetPlayerPed(-1)
                playerloc = GetEntityCoords(player)
                closestvehicle = GetClosestVehicle( playerloc.x, playerloc.y, playerloc.z, 13.2 , 0, 70)
                --GetClosestVehicle( playerloc.x, playerloc.y, playerloc.z, 13.2 , 1981688531 , 16390 )
                displaytext(closestvehicle)
        Wait(0)
        end
end)


function displaytext(texttodisplay)
        SetTextScale(1.5, 1.5)
        SetTextEntry("STRING")
        AddTextComponentString(texttodisplay)
        SetTextCentre(0)
        DrawText(0.025, 0.70)
end

And it works just fine. I’m going to assume there’s something wrong with the flags; I’ll play with it, and hopefully I’ll be able to turn up some more documentation…

Interesting - it looks like contrary to popular beliefs, it looks like the value:
16384 ALLLL THE WAY TO 21503 works to get planes…

Old me would somehow test this one by one, but I can try to build a forloop for this shite!

Wish me luck!

Okay! It looks like my currentstanding suspicions were correct. Using a rudimentary for loop within LUA, I was able to get all of the validplaneflags for “GetClosestVehicle” while a door was open and moving.

These flags will be uploaded as PlaneFlagsOnGroundMovingDoorOpen

PlaneFlagsOnGroundMovingDoorOpen (3.5 KB)

Interesting…so I’ve run my shitty forloop in as many scenrios I can think of and got a few lists similar to the above. My goal is to have the nearest vehicle in ALL scenarios, so I used the unix command comm to find the common lines between all 3 of the files I’ve generated, and cut the list down to 256 flags for the specific plane I’m trying to use.

The goal is to find a flag for GetClosestVehicle that will allow me to target my specific plane in ALL scenarios.

The result will live in the “AllCommonFlags” file!

AllCommonFlags (1.5 KB)

Im not sure if further testing will help me reduce my list further, but at least for now, I got the answer to my question…256 of them! LOL

I’ll keep on testing this!

Damn son!

It looks like after more testing, I was able to cut it down to 128 possible flags.
The change:
Testing this with an empty plane, engine off…

CutDownafterTestedVacant (768 Bytes)

you could just loop through the gamepool of CVehicle, then from their use the vehicle to check the model, class or even plate.

WHERE WERE YOU BEFORE?!?!?!??!

LOL Joking!
Never really heard of CVehicle. Google reveals it’s a headerfile (.h), but how does this play into lua? How can I test this directly? Are there any resources on reading / leveraging such files?

I’d love to know your opinion!

How to get all vehicles in a radius - FiveM Resource Development & Modding / Discussion - Cfx.re Community