GetNearestVehicle: Difference between revisions

From Onset Developer Wiki
(Created page with "{{Info|Lua Helper|Server & Client|-}} {{FuncDescription|Gets the nearest vehicle for a player.}} {{FuncSyntax|GetNearestVehicle(player)}} {{FuncParameters}} {{FuncParam|pla...")
 
No edit summary
 
Line 51: Line 51:


{{RelatedFunctions}}
{{RelatedFunctions}}
__EDIT_ME__
[[LuaHelper|A list of useful Lua functions. ]]

Latest revision as of 18:27, 30 August 2020

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.