# Detecting Vehicles

Hey,

I’ve been trying to select a vehicle at a given coordinate. However, none of the functions seem to be working. I have tried GetClosestVehicle, GetRandomVehicleInSphere, and IsAnyVehicleNearPoint. I have tried many different parameters for each. I have also tried on players and NPCs. No luck.

Anybody able to provide any insight?

Thanks

@Briglair “you tried”

Any samples?

@Tusticles Some of the things I have tried:

local closecar = GetClosestVehicle(coords2.x, coords2.y, coords2.z, 500, 0, 70)
local nearVehicle = IsAnyVehicleNearPoint(coords2.x, coords2.y, coords2.z, 50)
local gotVehicle, closecar = GetRandomVehicleInSphere(coords2.x, coords2.y, coords2.z, 50, 0, 70)

I’ve tried playing around with some of the coordinates and flags, but as I mentioned, I haven’t been able to get anything to work.

@Briglair how far you are from those coords?

@Tusticles Not far. Only about 10 units.

I have the script getting the offset slightly ahead of my player.

@Briglair have you seen this?

http://www.dev-c.com/nativedb/func/info/f73eb622c4f1689b

@Tusticles Yes. That is what I have been referencing for the most part. In addition, I have read all the additional documentation found in the links of that description.

@Briglair

I also tried something and it doesn’t work… sadly, worldGetAllVehicles is not implemented in fiver, at least not yet.

Here a small function that get the car in the direction you specify.

``````-- From point A to B -- Use GetOffsetFromEntityInWorldCoords for distance
function getVehicleInDirection(coordFrom, coordTo)
local rayHandle = CastRayPointToPoint(coordFrom.x, coordFrom.y, coordFrom.z, coordTo.x, coordTo.y, coordTo.z, 10, GetPlayerPed(-1), 0)
local a, b, c, d, vehicle = GetRaycastResult(rayHandle)
return vehicle
end
``````

Exemple:

``````if IsControlJustReleased(0, 51) then
local coordA = GetEntityCoords(GetPlayerPed(-1), 1)
local coordB = GetOffsetFromEntityInWorldCoords(GetPlayerPed(-1), 0.0, 20.0, 0.0)
local targetVehicle = getVehicleInDirection(coordA, coordB)
-- HOLY SHIT THE VEHICLE IS RIGHT HERE ---> targetVehicle
end
``````

GET_OFFSET_FROM_ENTITY_GIVEN_WORLD_COORDS
http://www.dev-c.com/nativedb/func/info/2274bc1c4885e333

_CAST_RAY_POINT_TO_POINT
http://www.dev-c.com/nativedb/func/info/377906d8a31e5586

_GET_RAYCAST_RESULT
http://www.dev-c.com/nativedb/func/info/3d87450e15d98694

@Tusticles I even tried that script he wrote up. I was converting it all to Lua then I got to that part and let out a long sigh. Haha.

@Konijima That code works and does exactly what I want it to do. Thanks.

``````local _, _, _, _, vehicle = GetRaycastResult(rayHandle)
``````

should be better, i think

Anyway, this code:

``````local pos = GetEntityCoords(GetPlayerPed(-1))
local vehicle = GetClosestVehicle(pos['x'], pos['y'], pos['z'], 5.001, 0, 70)
if DoesEntityExist(vehicle) then
-- do_something
end

``````

works fine.

and you can use this flag list:

``````00000000000000000 = 0 - Only returns cars and motorbikes. Often returns vehicle already inside. Otherwise only return empty vehicles.
00000000000000010 = 2 - Only empty vehicles. Only returns cars and motorbikes.
00000000000000100 = 4 - Works like 70. During the testing, this one worked the best i.e. for cars and motorbikes
00000000000000110 = 6 - Works like 70.
00000000000000111 = 7 - Works like 70.
00000000000010111 = 23 - Only finds cars when not inside one.
00000000001000110 = 70 - (Not from scripts. Recommended by the native db). Only works with motorbikes and cars.
00000000001111111 = 127 - While inside cars or motorbikes, nothing can be found. On foot or inside heli cars can be found.
00000000100000100 = 260 - Works like 70.
00000100001100010 = 2146
00000100001111111 = 2175
00011000000000110 = 12294
00100000000000000 = 16384
00100000000000010 = 16386
00101000000010111 = 20503
01000000000000000 = 32768
10000100000000110 = 67590
10000100001111111 = 67711 - Finds cars when inside heli, not when inside cars.
11000000000000101 = 98309
11000100000000111 = 100359 - Works like 70 but returns the vehicle the player already is in quite often.
11111111111111111 = 131071 - Not from the scripts. Nothing seems to work when testing.
00100000000000000 = 16384 - returns planes only.
``````
1 Like

71 returns police vehicles.

2 Likes

Hello, id to return some function GetClosestVehicle() correspond to what exactly? On an entity of type Player, or is PlayerPed, serverid or other?
How to transform it there playerPed please
i have tested GetPlayerServerId(), GetPlayerPed() and GetPlayerFromServerId()
for get player name (GetPlayerName()) dont work

How to return a boat?

did u found out for boat? or maybe heli/planes ? also look for it making heli/plane garage but cant store em atm

I am having a problem as well with trying to find the Plane/Heli & Boat Return.

I usually use the flag 12294 instead of 70 on get closest vehicle (cant remember why), though, with modded cars I can never use this method, only using cast rays which I have always reverted to using.

1 Like

Is it possible to detect a vehicule random, except one vehicle ?

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.

Thanks

IsAnyVehicleNearPoint() only works for me if the radius is a floating point number (50.0 in your example instead of 50).

I know this is probably not relevant for you anymore, but maybe this helps someone who has the same problem sometime.

1 Like