Created
September 20, 2022 11:06
-
-
Save thelindat/93311a4fd6ea6c1d4427438a533e228c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local ESX = exports.es_extended:getSharedObject() | |
local jobCount = {} | |
local function addJob(job) | |
jobCount[job.name] = (jobCount[job.name] or 0) + 1 | |
GlobalState[job.name] = jobCount[job.name] | |
end | |
local function playerLoaded(_, xPlayer) | |
local data = { | |
name = xPlayer.job.name, | |
onDuty = xPlayer.job.onDuty == nil or xPlayer.job.onDuty, | |
} | |
ESX.Players[xPlayer.source] = data | |
if data.onDuty then addJob(data) end | |
end | |
for i = 1, #ESX.Players do | |
playerLoaded(_, ESX.Players[i]) | |
end | |
AddEventHandler('esx:playerLoaded', playerLoaded) | |
local function removeJob(job) | |
jobCount[job.name] = (jobCount[job.name] or 1) - 1 | |
GlobalState[job.name] = jobCount[job.name] | |
end | |
AddEventHandler('esx:setJob', function(playerId, job) | |
local data = { | |
name = job.name, | |
onDuty = job.onDuty == nil or job.onDuty, | |
} | |
local lastJob = ESX.Players[playerId] | |
ESX.Players[playerId] = data | |
if job.name ~= lastJob.name then | |
if data.onDuty then addJob(data) end | |
if lastJob.onDuty then removeJob(lastJob) end | |
end | |
end) | |
AddEventHandler('esx:playerDropped', function(playerId) | |
local lastJob = ESX.Players[playerId] | |
ESX.Players[playerId] = nil | |
if lastJob.onDuty then removeJob(lastJob) end | |
end) | |
exports('getMembers', function(filter) | |
local type = type(filter) | |
local response = {} | |
if type == 'string' then | |
for playerId, job in pairs(ESX.Players) do | |
if job.name == filter and job.onDuty then | |
response[playerId] = job | |
end | |
end | |
elseif type == 'table' then | |
for playerId, job in pairs(ESX.Players) do | |
local match = filter[job.name] | |
if match == true and job.onDuty then | |
response[playerId] = job | |
elseif match == false then | |
response[playerId] = job | |
end | |
end | |
end | |
return response | |
end) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Can be executed in any resource, on the server only. | |
for playerId, job in pairs(exports.esx_jobcount:getMembers({ | |
police = true, -- return on-duty members of police | |
ambulance = false, -- return all members of ambulance (including off-duty) | |
})) do | |
print(playerId, job.name, job.onDuty) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment