FiveM Docs
Addons

esx_service

esx_service is the on/off duty system for ESX Legacy jobs. It limits how many players are on duty, tracks counts in GlobalState and notifies colleagues.

esx_service tracks which players of a job are on duty. Job resources such as esx_taxijob and esx_mechanicjob use it to cap the number of players in service, show how many colleagues are active and send a notification to everyone on duty.

Overview

FactValue
Resourceesx_service
SideServer (state, callbacks) and client (notification)
Dependenciesesx_lib, es_extended
SQLnone
Confignone
StateIn memory, reset on restart. Counts are mirrored to GlobalState[serviceName].

Installation

server.cfg
ensure esx_service
# job resources that use it
ensure esx_taxijob

A service name is the job name, for example taxi. A player can only enter the service of their own job.

Server API

All server events are local and protected: GetInvokingResource() must be set, so calls coming straight from a client are blocked and logged as Blocked external call.

esx_service:activateService

Creates a service and sets its player limit. Call it once on resource start.

TriggerEvent('esx_service:activateService', name, max)
NameTypeDescription
namestringService (job) name.
maxnumber or nilMaximum players on duty. -1 or a non-number means unlimited.
server/main.lua
TriggerEvent('esx_service:activateService', 'taxi', Config.MaxInService)

A service that was never activated is created on first use with no limit.

esx_service:disableService

Takes a player off duty.

TriggerEvent('esx_service:disableService', name, playerId)
NameTypeDescription
namestringService name.
playerIdnumberServer ID of the player.

esx_service:notifyAllInService

Sends an advanced notification to everyone on duty in a service, except the sender. The sender's mugshot is used as the icon.

TriggerEvent('esx_service:notifyAllInService', notification, name, sender)
NameTypeDescription
notificationtable{ title, subject, msg, iconType } passed to ESX.ShowAdvancedNotification.
namestringService name.
sendernumber or nilServer ID of the sender.
server/main.lua
TriggerEvent('esx_service:notifyAllInService', {
    title = 'Dispatch',
    subject = 'Backup',
    msg = 'Officer needs assistance at Legion Square',
    iconType = 1,
}, 'police', source)

GlobalState count

local onDuty = GlobalState.taxi or 0

The value updates on every enable, disable and disconnect.

Server callbacks

Call these from the client with ESX.TriggerServerCallback, ESX.AwaitServerCallback or xLib.callback. See Callbacks.

CallbackArgumentsReturnsCheck
esx_service:enableServicenamecanTakeService, maxInService, inServiceCountCaller's job must equal name; count must be below the limit.
esx_service:disableServicenamebooleanCaller's job must equal name.
esx_service:isInServicenamebooleanCaller only.
esx_service:isPlayerInServicename, targetIdbooleanCaller and target must both have job name.
esx_service:getInServiceListnametable { [serverId] = true }Caller's job must equal name.
client/main.lua
ESX.TriggerServerCallback('esx_service:enableService', function(canTakeService, maxInService, inServiceCount)
    if canTakeService then
        ESX.ShowNotification('You are now on duty')
    else
        ESX.ShowNotification(('Service full: %s/%s'):format(inServiceCount, maxInService))
    end
end, 'taxi')
client/main.lua
-- go off duty
ESX.TriggerServerCallback('esx_service:disableService', function() end, 'taxi')

-- check own duty state
local onDuty = ESX.AwaitServerCallback('esx_service:isInService', 'taxi')

Client event

esx_service:notifyAllInService (net, server to client) shows the notification. It is triggered by the server event above; you do not call it yourself.

Integration tips

  • esx_service is separate from the onDuty field of the ESX job object. If you also use xPlayer.setJob(job, grade, onDuty), keep both in sync yourself.
  • Players are removed from every service when they disconnect, but not when they change job. Call esx_service:disableService when you fire someone.
  • Duty state does not survive a restart of esx_service.

Common issues

  • enableService always returns false: the player's job name differs from the service name you pass.
  • Count above the limit: activateService ran after players were already on duty, or with a different max. Activate on resource start.
  • Old scripts use TriggerServerEvent('esx_service:disableService', ...): replace it with the callback.

Frequently asked questions

Independent documentation

esx-fivem.com is a community project and is not affiliated with the ESX Framework team. Facts on this page were checked against the upstream source. Official resources: docs.esx-framework.org and github.com/esx-framework.
Edit on GitHub

Last updated on

On this page