GetNearestVehicle

From Onset Developer Wiki
Revision as of 18:27, 30 August 2020 by BlueMountains (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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.