local vehHandle = nil
vehHandle = getNearestVehicle(300)
if vehHandle ~= nil and currentVehicle == nil then
setCarCollision(vehHandle, false)
currentVehicle = vehHandle
end
elseif currentVehicle ~= nil then
if isVehicleExist(currentVehicle) then
setCarCollision(currentVehicle, true)
currentVehicle = nil
end
end
function getNearestVehicle(radius)
if not sampIsLocalPlayerSpawned() then return end
local pVehicle = getLocalVehicle()
local pCoords = {getCharCoordinates(PLAYER_PED)}
local vehicles = getAllVehicles()
table.sort(vehicles, function(a, b)
local aX, aY, aZ = getCarCoordinates(a)
local bX, bY, bZ = getCarCoordinates(b)
return getDistanceBetweenCoords3d(aX, aY, aZ, unpack(pCoords)) < getDistanceBetweenCoords3d(bX, bY, bZ, unpack(pCoords))
end)
for i = #vehicles, 1, -1 do
if vehicles[i] == pVehicle then
table.remove(vehicles, i)
elseif radius ~= nil then
local x, y, z = getCarCoordinates(vehicles[i])
if getDistanceBetweenCoords3d(x, y, z, unpack(pCoords)) > radius then
table.remove(vehicles, i)
end
end
end
return vehicles[1]
end