Multi Theft Auto: Wiki:GetNearestVehicle
		
		
		
		Jump to navigation
		Jump to search
		
Syntax
vehicle getNearestVehicle( element thePlayer, float distance )
Required Arguments
- thePlayer: The player you want to get the nearest vehicle of.
- distance: The distance to search for vehicles in.
Returns
Return a vehicle element if success, returns false if thers no vehicles in the specified circle.
Code
Click to collapse [-]
Server- and/or clientside Scriptfunction getNearestVehicle(player,distance)
	local tempTable = {}
	local lastMinDis = distance-0.0001
	local nearestVeh = false
	local px,py,pz = getElementPosition(player)
	for _,v in pairs(getElementsByType("vehicle")) do
		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
	return nearestVeh
end