Check active police officers in server with command

If you want to check how many police officers there are in the server you can paste the code below in server.lua

QBCore.Commands.Add('checkcop', "Checks police officers in duty", {}, true, function(source)
    local xplayer = QBCore.Functions.GetPlayers()

    local cops = 0
    for i=1, #xplayer, 1 do
        local xplayer = QBCore.Functions.GetPlayer(xplayer[i])
        if xplayer.PlayerData.job.name == 'police' then
            cops = cops + 1
        end
    end
    print("There are "..cops.." online")
end, "god")

The code is only for QBCore, and only for permission “god”, you can change it to anything you want.
Configure the jobs name to your police one, Also, you can check how many people are in the server from a certian job with this code, just change the job name to your desired one.

1 Like
local framework = "esx" -- esx or qbcore

local Jobs = {
    "police",
    "fib",
    "ambulance",
    -- add more jobs here
}

local allowedGroups = {
    ["superadmin"] = true,
    ["admin"] = true,
    ["mod"] = true,
}

local function isAllowed(group)
    return allowedGroups[group] == true
end

local function countPlayersByJob(jobName)
    local count = 0
    if framework == "esx" then
        count = #ESX.GetExtendedPlayers('job', jobName)
    else
        local players = QBCore.Functions.GetPlayers()
        for i = 1, #players, 1 do
            local player = QBCore.Functions.GetPlayer(players[i])
            if player and player.PlayerData.job.name == jobName then
                count = count + 1
            end
        end
    end
    return count
end

RegisterCommand('checkJobs', function(source, args, rawCommand)
    local xPlayer, xPerm

    if framework == "esx" then
        xPlayer = ESX.GetPlayerFromId(source)
        xPerm = xPlayer.getGroup()
    else
        xPlayer = QBCore.Functions.GetPlayer(source)
        xPerm = xPlayer.PlayerData.job.grade.name
    end

    if not isAllowed(xPerm) then return end

    if args[1] == "all" or not args[1] then
        for _, job in pairs(Jobs) do
            local count = countPlayersByJob(job)
            print(job..": "..count.." online")
        end
    else
        local count = countPlayersByJob(args[1])
        print("There are "..count.." "..args[1].." online")
    end
end, false)


Which resource is this going in? QBCore or the QB-policejob

Hi, it goes to any resource you want, just paste it in the client to the client and the server to the server.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.