diff --git a/[core]/es_extended/server/functions.lua b/[core]/es_extended/server/functions.lua index 606ab0e74..cec340b53 100644 --- a/[core]/es_extended/server/functions.lua +++ b/[core]/es_extended/server/functions.lua @@ -276,15 +276,15 @@ end ESX.GetPlayers = GetPlayers -local function checkTable(key, val, player, xPlayers) +local function checkTable(key, val, xPlayer, xPlayers) for valIndex = 1, #val do local value = val[valIndex] if not xPlayers[value] then xPlayers[value] = {} end - if (key == "job" and player.job.name == value) or player[key] == value then - xPlayers[value][#xPlayers[value] + 1] = player + if (key == "job" and xPlayer.job.name == value) or xPlayer[key] == value then + xPlayers[value][#xPlayers[value] + 1] = xPlayer end end end @@ -293,20 +293,22 @@ end ---@param val? string|table ---@return table function ESX.GetExtendedPlayers(key, val) + if not key then + return ESX.Table.ToArray(ESX.Players) + end + local xPlayers = {} if type(val) == "table" then - for _, v in pairs(ESX.Players) do - checkTable(key, val, v, xPlayers) + for i, xPlayer in pairs(ESX.Players) do + checkTable(key, val, xPlayer, xPlayers) end - else - for _, v in pairs(ESX.Players) do - if key then - if (key == "job" and v.job.name == val) or v[key] == val then - xPlayers[#xPlayers + 1] = v - end - else - xPlayers[#xPlayers + 1] = v - end + + return xPlayers + end + + for i, xPlayer in pairs(ESX.Players) do + if (key == "job" and xPlayer.job.name == val) or xPlayer[key] == val then + xPlayers[#xPlayers + 1] = xPlayer end end diff --git a/[core]/es_extended/shared/modules/table.lua b/[core]/es_extended/shared/modules/table.lua index fc3452005..aacb25840 100644 --- a/[core]/es_extended/shared/modules/table.lua +++ b/[core]/es_extended/shared/modules/table.lua @@ -223,3 +223,11 @@ function ESX.Table.Sort(t, order) end end end + +function ESX.Table.ToArray(table) + local array = {} + for _, v in pairs(table) do + array[#array + 1] = v + end + return array +end