GetNearestVehicle

From Onset Developer Wiki
Revision as of 20:54, 9 August 2019 by BlueMountains (talk | contribs) (Created page with "{{Info|Lua Helper|Server & Client|-}} {{FuncDescription|Gets the nearest vehicle for a player.}} {{FuncSyntax|GetNearestVehicle(player)}} {{FuncParameters}} {{FuncParam|pla...")
(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

__EDIT_ME__