diff --git a/client/functions.lua b/client/functions.lua index 6df151f..0ff2ce1 100644 --- a/client/functions.lua +++ b/client/functions.lua @@ -177,29 +177,37 @@ function QBCore.Functions.GetPlayersFromCoords(coords, distance) return closePlayers end -function QBCore.Functions.GetClosestVehicle() - local ped = PlayerPedId() - local coords = GetEntityCoords(ped) - local vehicles = GetGamePool('CVehicle') - local closestDistance = -1 - local closestVehicle = -1 - for i = 1, #vehicles, 1 do - local vehicleCoords = GetEntityCoords(vehicles[i]) - local distance = #(vehicleCoords - coords) - if closestDistance == -1 or closestDistance > distance then - closestVehicle = vehicles[i] - closestDistance = distance - end - end - return closestVehicle, closestDistance +function QBCore.Functions.GetClosestVehicle(coords) + local vehicles = GetGamePool('CVehicle') + local closestDistance = -1 + local closestVehicle = -1 + local coords = coords + + if coords == nil then + local playerPed = PlayerPedId() + coords = GetEntityCoords(playerPed) + end + for i=1, #vehicles, 1 do + local vehicleCoords = GetEntityCoords(vehicles[i]) + local distance = #(vehicleCoords - coords) + + if closestDistance == -1 or closestDistance > distance then + closestVehicle = vehicles[i] + closestDistance = distance + end + end + return closestVehicle,closestDistance end -function QBCore.Functions.GetClosestObject() +function QBCore.Functions.GetClosestObject(coords) local ped = PlayerPedId() - local coords = GetEntityCoords(ped) local objects = GetGamePool('CObject') local closestDistance = -1 local closestObject = -1 + local coords = coords + if coords == nil then + coords = GetEntityCoords(ped) + end for i = 1, #objects, 1 do local objectCoords = GetEntityCoords(objects[i]) local distance = #(objectCoords - coords) @@ -208,7 +216,7 @@ function QBCore.Functions.GetClosestObject() closestDistance = distance end end - return closestObject, closestDistance + return closestObject,closestDistance end -- Vehicle