Untitled conversation
- User
function getNearestVehicle() local vehicles = getAllVehicles() if #vehicles == 0 then return nil end local playerX, playerY, playerZ = getCharCoordinates(PLAYER_PED) local nearestVehicle = nil local minDistanceSquared = nil local MAX_DISTANCE_SQUARED = 10000 -- Максимальное расстояние 100 метров (10 000 кв.м) for _, vehicle in ipairs(vehicles) do if not isCarDead(vehicle) then local vehX, vehY, vehZ = getCarCoordinates(vehicle) local dx = playerX - vehX local dy = playerY - vehY local dz = playerZ - vehZ local distanceSquared = dx * dx + dy * dy + dz * dz if distanceSquared <= MAX_DISTANCE_SQUARED then if minDistanceSquared == nil or distanceSquared < minDistanceSquared then minDistanceSquared = distanceSquared nearestVehicle = vehicle end end end end return nearestVehicle endDouble if statement. Please combine the condition of this if statement with that of the outer if statement using
and. fix that - Assistant
Certainly!
In your current code, you have a nested
ifstatement inside anotherifstatement:if distanceSquared <= MAX_DISTANCE_SQUARED then if minDistanceSquared == nil or distanceSquared < minDistanceSquared then -- code end end