GetClosestVehicle is 0

I have a small problem with the function “GetClosestVehicle” it returns me 0 for a vehicle that I have made appear by hand. But the natural vehicle they look good value.



Get Vehicle infos
local pos = GetEntityCoords(player) Citizen.Trace("----- Debug : " .. pos.x .. " " .. pos.y .. " " .. pos.z .. "----- \n") local vehicleHandle = GetClosestVehicle(pos.x, pos.y, pos.z, 3.0, 0, 70) if vehicleHandle ~= nil then Citizen.Trace("----- Debug : " .. vehicleHandle .. "----- \n") end

Spawn Vehicle:

`local playerCoords = GetEntityCoords(playerPed)

	local veh = CreateVehicle(carid, playerCoords, 0.0, true, false)

SetVehicleHasBeenOwnedByPlayer(veh, true)

local id = NetworkGetNetworkIdFromEntity(veh)
SetNetworkIdCanMigrate(id, true)
Citizen.InvokeNative(0x629BFA74418D6239, Citizen.PointerValueIntInitialized(veh))

	TaskWarpPedIntoVehicle(playerPed, veh, -1)
	SetEntityInvincible(veh, false)`
1 Like

GetClosestVehicle did not return any ELS vehicles, you must use something like GetRaycastResult


I tried using GetClosestVehicle for my delete vehicle script, but it never worked. Use a raycast.

1 Like

According to the NativeDB:

Example usage
VEHICLE::GET_CLOSEST_VEHICLE(x, y, z, radius, hash, unknown leave at 70)

x, y, z: Position to get closest vehicle to.
radius: Max radius to get a vehicle.
modelHash: Limit to vehicles with this model. 0 for any.
flags: The bitwise flags altering the function’s behaviour.

Does not return police cars or helicopters.

It seems to return police cars for me, does not seem to return helicopters, planes or boats for some reason

Only returns non police cars and motorbikes with the flag set to 70 and modelHash to 0. ModelHash seems to always be 0 when not a modelHash in the scripts, as stated above.

These flags were found in the b617d scripts: 0,2,4,6,7,23,127,260,2146,2175,12294,16384,16386,20503,32768,67590,67711,98309,100359.
Converted to binary, each bit probably represents a flag as explained regarding another native here: [Guide] Driving Styles - Documentation - GTAForums

Conversion of found flags to binary: VEHICLE::GET_CLOSEST_VEHICLE - found flags in binary -

At exactly 16384 which is 0100000000000000 in binary and 4000 in hexadecimal only planes are returned.

It’s probably more convenient to use worldGetAllVehicles(int *arr, int arrSize) and check the shortest distance yourself and sort if you want by checking the vehicle type with for example VEHICLE::IS_THIS_MODEL_A_BOAT

Police vehicles are not returned by GetClosestVehicle. However, I have no idea why it is not returning normal vehicles.


GetRaycastResult resolve probleme thx guys

Can you share what you did? Im actually interested in using this for something else.

Yes off course

    local pos = GetEntityCoords(player)
    local entityWorld = GetOffsetFromEntityInWorldCoords(player, 0.0, 20.0, 0.0)

    local rayHandle = CastRayPointToPoint(pos.x, pos.y, pos.z, entityWorld.x, entityWorld.y, entityWorld.z, 10, player, 0)
    local a, b, c, d, vehicleHandle = GetRaycastResult(rayHandle)

    if vehicleHandle ~= nil then

Why does that happen?

Already replyied to that question

I think he means why did you say it doesnt detect ELS vehicles? Really it should…

Oh now its an understandable question, actually I don’t know, I think who did GTA5 API thinked this will be a good idea

‘ELS’ vehicles are marked as mission vehicles, and it’d be a bit silly if, as a scripter, you’d look for a vehicle, and find a vehicle you just created yourself that you already know of.



Is it possible to detect a random vehicle, except the flatbed ?

I want to put a car on the flatbed, si i want to check if vehicle ~= flatbed then pur the other vehicle on the flatbed.


I’m trying to get my code to find a vehicle the player has never entered,
but this vehicle is in the player’s possession, it’s a police vehicle.

I want a function on this vehicle without theoretical or player to fall,
I just want the fact that the player is close to the vehicle and so call the function

if IsVehicleSirenOn(vehicle) and IsVehicleSirenAudioOn(vehicle) then
elseif IsVehicleSirenOn(vehicle) and not IsVehicleSirenAudioOn(vehicle) then

What I’ve already managed to make work: If the player turns on the siren sound it will stay on regardless of whether it is inside or outside the vehicle.

What I couldn’t work: If the player turns off the siren sound and enters another vehicle and after that returns to the vehicle the siren remains muted.

In the code I wrote the siren is historical if the player is in or out of the vehicle, but if the player turns off and exits the vehicle and enters another vehicle and after that goes back to the vehicle, the siren that should be muted turns on fully, I need help to fix this.

– my english is rusty, i hope you understand