GetNearestVehicle: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
Line 16: Line 16:
local nearestVeh = false
local nearestVeh = false
local px,py,pz = getElementPosition(player)
local px,py,pz = getElementPosition(player)
local pint = getElementInteiror(player)
local pdim = getElementDimension(player)
for _,v in pairs(getElementsByType("vehicle")) do
for _,v in pairs(getElementsByType("vehicle")) do
local vx,vy,vz = getElementPosition(v)
local vint,vdim = getElementInterior(player),getElementDimension(player)
local dis = getDistanceBetweenPoints3D(px,py,pz,vx,vy,vz)
if vint == pint and vdim == pdim then
if dis < distance then
local vx,vy,vz = getElementPosition(v)
if dis < lastMinDis then  
local dis = getDistanceBetweenPoints3D(px,py,pz,vx,vy,vz)
lastMinDis = dis
if dis < distance then
nearestVeh = v
if dis < lastMinDis then  
lastMinDis = dis
nearestVeh = v
end
end
end
end
end
Line 28: Line 34:
return nearestVeh
return nearestVeh
end
end
</syntaxhighlight></section>
</syntaxhighlight></section>

Revision as of 08:18, 10 September 2017

Syntax

vehicle getNearestVehicle( element thePlayer )

Required Arguments

  • thePlayer: The player you want to get the nearest vehicle of.


Returns

Return a vehicle element if success, false if there's no vehicles in a 10meter circle..

Code

Click to collapse [-]
Server- and/or clientside Script
function getNearestVehicle(player,distance)
	local tempTable = {}
	local lastMinDis = distance-0.0001
	local nearestVeh = false
	local px,py,pz = getElementPosition(player)
	local pint = getElementInteiror(player)
	local pdim = getElementDimension(player)

	for _,v in pairs(getElementsByType("vehicle")) do
		local vint,vdim = getElementInterior(player),getElementDimension(player)
		if vint == pint and vdim == pdim then
			local vx,vy,vz = getElementPosition(v)
			local dis = getDistanceBetweenPoints3D(px,py,pz,vx,vy,vz)
			if dis < distance then
				if dis < lastMinDis then 
					lastMinDis = dis
					nearestVeh = v
				end
			end
		end
	end
	return nearestVeh
end