VehicleClient

getClosestVehicle

GET_CLOSEST_VEHICLE0xF73EB622C4F1689B

The GTA V native GET_CLOSEST_VEHICLE of the VEHICLE namespace, as LS:MP client scripts call it: the global function getClosestVehicle, in JavaScript and in Lua.

SignatureJavaScript
getClosestVehicle(
  x: number,
  y: number,
  z: number,
  radius: number,
  modelHash: Hash,
  flags: number
): Vehicle
SignatureLua
getClosestVehicle(
  x: number,
  y: number,
  z: number,
  radius: number,
  modelHash: Hash,
  flags: integer
): Vehicle

Usage

JavaScript
const vehicle = getClosestVehicle(x, y, z, radius, modelHash, flags);
Lua
local vehicle = getClosestVehicle(x, y, z, radius, modelHash, flags)

Three consecutive numbers can be given as one vector: { x, y, z } (or [x, y, z]) in JavaScript, a Vector3 in Lua.

JavaScript · with vectors
const vehicle = getClosestVehicle(pos, radius, modelHash, flags);
Lua · with vectors
local vehicle = getClosestVehicle(pos, radius, modelHash, flags)

Parameters

ParameterTypeJavaScriptLuaDescription
xfloatnumbernumberPosition to get closest vehicle to.
yfloatnumbernumberPosition to get closest vehicle to.
zfloatnumbernumberPosition to get closest vehicle to.
radiusfloatnumbernumberMax radius to get a vehicle.
modelHashHashnumberintegerLimit to vehicles with this model. 0 for any.
flagsintnumberintegerThe bitwise flags altering the function's behaviour.

Returns

Vehicle JavaScript number, Lua integer

Game handle of a vehicle. Not an LS:MP netId. Convert one with lsmp.getEntityFromNetId.

Notes

From the community native database. Names are LS:MP’s; samples may be written for C++ or another framework.

Example usage
getClosestVehicle(x, y, z, radius, hash, unknown leave at 70)

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: https://gtaforums.com/topic/822314-guide-driving-styles

Conversion of found flags to binary: https://pastebin.com/kghNFkRi

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 isThisModelABoat


Conclusion: This native is not worth trying to use. Use something like this instead: https://pastebin.com/xiFdXa7h

Full list of vehicles by DurtyFree: https://github.com/DurtyFree/gta-v-data-dumps/blob/master/vehicles.json

Reference

Namespace
Vehicle VEHICLE
In the game
Vehicle GET_CLOSEST_VEHICLE(float x, float y, float z, float radius, Hash modelHash, int flags)
Hash
0xF73EB622C4F1689B

Open in the interactive natives reference