GetNearestVehicle

From Onset Developer Wiki
GetNearestVehicle

Type: Lua Helper
Context: Server & Client
Introduced: v-

Description

Gets the nearest vehicle for a player.

Syntax

GetNearestVehicle(player)

Parameters

  • player
    The player identifier.

Return Value

  • Returns the vehicle identifier and the distance. Returns 0 if there is no vehicle streamed.

Example Server

function GetNearestVehicle(player)
	local vehicles = GetStreamedVehiclesForPlayer(player)
	local found = 0
	local nearest_dist = 999999.9
	local x, y, z = GetPlayerLocation(player)

	for _,v in pairs(vehicles) do
		local x2, y2, z2 = GetVehicleLocation(v)
		local dist = GetDistance3D(x, y, z, x2, y2, z2)
		if dist < nearest_dist then
			nearest_dist = dist
			found = v
		end
	end
	return found, nearest_dist
end

Example Client

function GetNearestVehicle()
	local vehicles = GetStreamedVehicles()
	local found = 0
	local nearest_dist = 999999.9
	local x, y, z = GetPlayerLocation()

	for _,v in pairs(vehicles) do
		local x2, y2, z2 = GetVehicleLocation(v)
		local dist = GetDistance3D(x, y, z, x2, y2, z2)
		if dist < nearest_dist then
			nearest_dist = dist
			found = v
		end
	end
	return found, nearest_dist
end

See also

A list of useful Lua functions.