Skip to content

Commit 7a0567e

Browse files
committed
feat(core): add functions to retrieve online players by job name or type with optional on-duty check
Adds two utility functions to retrieve online players by either job name or job type, with optional filtering for on-duty status. While the logic is a bit nested, this approach avoids unnecessary function splitting and keeps related behavior in a single place. Although I'm not entirely satisfied with the nesting, this structure felt like a reasonable trade-off between readability and reusability. This method could also serve as a foundation for deprecating older job-check functions, should the maintainer choose to consolidate similar logic in the future.
1 parent fdd380e commit 7a0567e

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

server/functions.lua

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,52 @@ function QBCore.Functions.GetPlayersOnDuty(job)
150150
return players, count
151151
end
152152

153+
---Gets a list of all online players of a specified job and the number, with an option to check if they are on duty.
154+
---@param job string
155+
---@param checkOnDuty boolean
156+
---@return table, number
157+
function QBCore.Functions.GetPlayersByJobName(job, checkOnDuty)
158+
local players = {}
159+
local count = 0
160+
for src, Player in pairs(QBCore.Players) do
161+
if Player.PlayerData.job.name == job then
162+
if checkOnDuty then
163+
if Player.PlayerData.job.onduty then
164+
players[#players + 1] = src
165+
count += 1
166+
end
167+
else
168+
players[#players + 1] = src
169+
count += 1
170+
end
171+
end
172+
end
173+
return players, count
174+
end
175+
176+
---Gets a list of all online players with a specified job type and the number, with an option to check if they are on duty.
177+
---@param jobType string
178+
---@param checkOnDuty boolean
179+
---@return table, number
180+
function QBCore.Functions.GetPlayersByJobType(jobType, checkOnDuty)
181+
local players = {}
182+
local count = 0
183+
for src, Player in pairs(QBCore.Players) do
184+
if Player.PlayerData.job.type == jobType then
185+
if checkOnDuty then
186+
if Player.PlayerData.job.onduty then
187+
players[#players + 1] = src
188+
count += 1
189+
end
190+
else
191+
players[#players + 1] = src
192+
count += 1
193+
end
194+
end
195+
end
196+
return players, count
197+
end
198+
153199
---Returns only the amount of players on duty for the specified job
154200
---@param job string
155201
---@return number

0 commit comments

Comments
 (0)