Change alert to all police that are on duty instead of only players in police cars

Hi guys im pretty new to coding. I have a shooting-alert script for the police that gives them a notification when someone shoots in the server, the script however is written in a way that only players who are sitting in a policevehicle will get a notification. I would like this to change for all police officers who are currently on duty.

-- Notify player
RegisterNetEvent('gunshotNotify')
AddEventHandler('gunshotNotify', function(alert)
    Wait(Config.NotifyTime)
    if IsPedInAnyPoliceVehicle(PlayerPedId()) then
        PlaySoundFrontend(-1, "TIMER_STOP", "HUD_MINI_GAME_SOUNDSET", 1)
        Notify(alert)
    end
end)

-- Show blip on map
RegisterNetEvent('gunshotLocation')
AddEventHandler('gunshotLocation', function(gx, gy, gz)
    Wait(Config.NotifyTime)
    if IsPedInAnyPoliceVehicle(PlayerPedId()) then 
        local gunshotBlip = AddBlipForRadius(gx, gy, gz, Config.BlipRadius)
        SetBlipSprite(gunshotBlip, 161)
        SetBlipColour(gunshotBlip, Config.BlipColor)
        SetBlipAsShortRange(gunshotBlip, false)
        Citizen.Wait(Config.BlipTime)
        RemoveBlip(gunshotBlip)
    end
end)

I have been trying for hours getting my head around something like if PlayerJob = ‘police’ and PlayerJob.onduty = true then (the rest) But none of that i’m getting to work. I am running on QBcore, any help would be lovely :slight_smile:

1 Like

I mean, just remove the if statements that check whether the ped is in a police vehicle? Don’t forget to remove its ends too, BTW.

1 Like
QBCore = exports['qb-core']:GetCoreObject()

-- if officer (police\sherif\fib or some black list job)
local is_officer = false

RegisterNetEvent('QBCore:Client:OnPlayerLoaded', function()
	job = QBCore.Functions.GetPlayerData().job.name
	-- black list job
	if job == "police" or job == "ambulance" then
		is_officer = true
	end
end)

AddEventHandler('CEventGunShot', function(witnesses, ped)
	if is_officer then
		return
	end
	
	-- you shoot event HERE
end)
1 Like

try this

--Function to notify all on-duty police officers
function NotifyOnDutyPolice(message)
    local players = QBCore.Functions.GetPlayers() -- Get all active players
    for _, playerId in ipairs(players) do
        local player = QBCore.Functions.GetPlayer(playerId) -- Get player object
        if player then
            local job = player.PlayerData.job
            if job and job.name == "police" and job.onduty then
                -- Send notification to on-duty police officer
                TriggerClientEvent('QBCore:Notify', playerId, message, "success") -- "success" can be replaced with "info", "error", etc.
            end
        end
    end
end

-- Example event to trigger the notification
RegisterNetEvent('event:notifyPolice')
AddEventHandler('event:notifyPolice', function(eventDetails)
    local message = "Attention all units: " .. eventDetails
    NotifyOnDutyPolice(message)
end)

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