improvement(esx): Allow retrieval of ESX.Players with specific data

This commit is contained in:
Linden
2021-06-30 02:20:44 +10:00
parent c5611f47b4
commit b931e33b63
2 changed files with 23 additions and 4 deletions
+15 -2
View File
@@ -3,14 +3,27 @@ AddEventHandler('esx:playerLoaded', function(playerId, xPlayer, isNew)
ESX.Players[playerId] = xPlayer.job.name
end)
RegisterNetEvent('esx:setJob') -- When a player loads in, we can store some basic information about them locally
AddEventHandler('esx:setJob', function(playerId, job, lastJob)
ESX.Players[playerId] = job.name
end)
AddEventHandler('esx:playerDropped', function(playerId, reason) -- Remove the data once the player no longer exists
ESX.Players[playerId] = nil
end)
AddEventHandler('onResourceStart', function(resourceName) -- The resource just restarted so we actually have a fresh copy of ESX.Players from the framework
if (GetCurrentResourceName() == resourceName) then
for _, xPlayer in pairs(ESX.Players) do
if (GetCurrentResourceName() == resourceName) then -- Useful if we need to run functions or send events after a restart
for playerId, xPlayer in pairs(ESX.Players) do
print( ('%s %s is online with player id %s'):format(xPlayer.job.grade_label, xPlayer.name, playerId) )
end
end
end)
Citizen.CreateThread(function()
Citizen.Wait(1000)
local xPlayers = ESX.GetExtendedPlayers('job', 'police') -- New hitchless xPlayer loop, with the ability to only return players with specific data
for _, xPlayer in pairs(xPlayers) do
print(xPlayer.source, xPlayer.job.grade_label, xPlayer.name)
end
end)