From 39b55b27fd4d6a67603340fe6851b868405e3e2d Mon Sep 17 00:00:00 2001 From: Kakarot <57848836+GhzGarage@users.noreply.github.com> Date: Sat, 25 Sep 2021 21:41:06 -0600 Subject: [PATCH] fix(client/functions.lua): Closest Ped Scripts like qb-drugs corner selling require finding the closest ped but include a list of peds to ignore, typically your own. --- client/functions.lua | 56 +++++++++++++++++++++++++++++--------------- 1 file changed, 37 insertions(+), 19 deletions(-) diff --git a/client/functions.lua b/client/functions.lua index b3cd466..6df151f 100644 --- a/client/functions.lua +++ b/client/functions.lua @@ -100,10 +100,46 @@ end -- Getters function QBCore.Functions.GetVehicles() return GetGamePool('CVehicle') end -function QBCore.Functions.GetPeds() return GetGamePool('CPed') end function QBCore.Functions.GetObjects() return GetGamePool('CObject') end function QBCore.Functions.GetPlayers() return GetActivePlayers() end +function QBCore.Functions.GetPeds(ignoreList) + local pedPool = GetGamePool('CPed') + local ignoreList = ignoreList or {} + local peds = {} + for i = 1, #pedPool, 1 do + local found = false + for j=1, #ignoreList, 1 do + if ignoreList[j] == pedPool[i] then + found = true + end + end + if not found then + peds[#peds+1] = pedPool[i] + end + end + return peds +end + +function QBCore.Functions.GetClosestPed(coords, ignoreList) + local ped = PlayerPedId() + if not coords then coords = GetEntityCoords(ped) end + local ignoreList = ignoreList or {} + local peds = QBCore.Functions.GetPeds(ignoreList) + local closestDistance = -1 + local closestPed = -1 + for i = 1, #peds, 1 do + local pedCoords = GetEntityCoords(peds[i]) + local distance = #(pedCoords - coords) + + if closestDistance == -1 or closestDistance > distance then + closestPed = peds[i] + closestDistance = distance + end + end + return closestPed, closestDistance +end + function QBCore.Functions.GetClosestPlayer(coords) if coords == nil then coords = GetEntityCoords(PlayerPedId()) @@ -175,24 +211,6 @@ function QBCore.Functions.GetClosestObject() return closestObject, closestDistance end -function QBCore.Functions.GetClosestPed() - local ped = PlayerPedId() - local coords = GetEntityCoords(ped) - local peds = GetGamePool('CPed') - local closestDistance = -1 - local closestPed = -1 - for i = 1, #peds, 1 do - local pedCoords = GetEntityCoords(peds[i]) - local distance = #(pedCoords - coords) - - if closestDistance == -1 or closestDistance > distance then - closestPed = peds[i] - closestDistance = distance - end - end - return closestPed, closestDistance -end - -- Vehicle local function Round(value, numDecimalPlaces)