function getDistanceBetweenCoords2d(x1, y1, x2, y2)
return math.sqrt((x2-x1)^2 + (y2-y1)^2)
end
function getClosestVehicleCoords()
local botX, botY, _ = getBotPosition()
local vehicles = getAllVehicles()
local closestX, closestY, closestZ
for _, v in pairs(vehicles) do
if v.position then
local pos = v.position
if not closestX or getDistanceBetweenCoords2d(botX, botY, pos.x, pos.y) < getDistanceBetweenCoords2d(botX, botY, closestX, closestY) then
closestX, closestY, closestZ = pos.x, pos.y, pos.z
end
end
end
return closestX, closestY, closestZ
end
Usage:
local x, y, z = getClosestVehicleCoords()
if x then
print(x, y, z)
end