Client Functions
Reference for ESX Legacy client functions such as ESX.IsPlayerLoaded, ESX.GetPlayerData, ESX.SearchInventory and ESX.SecureNetEvent, with examples.
These are the general-purpose functions that es_extended adds to the ESX object on the client. Use them to check whether the character is loaded, read and update the cached player data, search the built-in inventory, register secure net events and key bindings.
Overview
| Resource | es_extended |
| Side | Client |
| Source | client/modules/player/*.lua, client/modules/core/events.lua, client/compat.lua, client/modules/game/*.lua |
| Access | ESX = exports['es_extended']:getSharedObject() or shared_script '@es_extended/imports.lua' |
UI helpers (notifications, menus, context, progress bar) are on Client UI. Entity helpers are on ESX.Game.
Client fields
These values are set when es_extended starts on the client.
| Field | Type | Description |
|---|---|---|
ESX.PlayerData | table | Cached data of the local character. See Client PlayerData. |
ESX.PlayerLoaded | boolean | true after esx:playerLoaded has been handled, false after logout. |
ESX.playerId | number | Local player index (PlayerId()). |
ESX.serverId | number | Server id of the local player (GetPlayerServerId(PlayerId())). |
Player data
ESX.IsPlayerLoaded
ESX.IsPlayerLoaded()Returns whether the local character is fully loaded. Check this before you read ESX.PlayerData.
Returns: boolean
CreateThread(function()
while not ESX.IsPlayerLoaded() do
Wait(250)
end
print(('Loaded as %s'):format(ESX.PlayerData.job.label))
end)ESX.GetPlayerData
ESX.GetPlayerData()Returns the ESX.PlayerData table. It is the same table reference, so reading ESX.PlayerData directly is equivalent.
Returns: table
local data = ESX.GetPlayerData()
print(data.identifier, data.job.name, data.job.grade_label)ESX.SetPlayerData
ESX.SetPlayerData(key, val)Writes a value into the local ESX.PlayerData and fires the local esx:setPlayerData event so other scripts (and OnPlayerData from the imports file) can react. The event is not fired for the loadout key, or when a non-table value did not change.
| Name | Type | Description |
|---|---|---|
key | string | Key in ESX.PlayerData to set. |
val | any | New value. |
Returns: nothing
This only changes the client cache. It does not save anything on the server and the server will overwrite the key the next time it sends an update. Change real data (job, money, items) on the server through the xPlayer object.
ESX.SetPlayerData('isCuffed', true)ESX.GetAccount
ESX.GetAccount(account)Returns one entry from ESX.PlayerData.accounts.
| Name | Type | Description |
|---|---|---|
account | string | Account name, for example money, bank or black_money. |
Returns: table|nil with name, money, label, round and index, or nil if the account does not exist.
local bank = ESX.GetAccount('bank')
if bank then
print(('Bank balance: $%s'):format(ESX.Math.GroupDigits(bank.money)))
endESX.SearchInventory
ESX.SearchInventory(items, count)Searches ESX.PlayerData.inventory (built-in ESX inventory only).
| Name | Type | Description |
|---|---|---|
items | string | string[] | One item name, or an array of item names. |
count | boolean? | When true, return counts instead of item tables. Missing items return 0. |
Returns:
- with a single item name: the item table (
name,count,label,weight,usable,rare,canRemove), its count, ornil. - with an array: a table keyed by item name.
local counts = ESX.SearchInventory({ 'bread', 'water' }, true)
print(('bread: %d, water: %d'):format(counts.bread, counts.water))
local bread = ESX.SearchInventory('bread')
if bread then
print(bread.label, bread.count)
endESX.ShowInventory
ESX.ShowInventory()Opens the default inventory UI through exports.esx_inventory:ShowInventory(). Does nothing unless Config.EnableDefaultInventory is enabled.
Returns: nothing
Events and spawning
ESX.SecureNetEvent
ESX.SecureNetEvent(name, func)Registers a client net event that only runs when the server triggered it. Calls from a local TriggerEvent (where source is an empty string) are ignored. Handlers are removed automatically when the registering resource stops. Errors inside the handler are re-thrown with the original message.
| Name | Type | Description |
|---|---|---|
name | string | Event name. |
func | function | Handler, receives the event arguments. |
Returns: nothing
ESX.SecureNetEvent('myjob:setDuty', function(onDuty)
ESX.ShowNotification(onDuty and 'You are on duty' or 'You are off duty')
end)TriggerClientEvent('myjob:setDuty', source, true)Use it for every client event that changes state or grants something. A plain RegisterNetEvent handler can also be fired locally by any client script.
ESX.DisableSpawnManager
ESX.DisableSpawnManager()Turns off auto-spawn of the default spawnmanager resource if it is started. ESX calls this itself before it spawns the player.
Returns: nothing
ESX.SpawnPlayer
ESX.SpawnPlayer(skin, coords, cb)Loads a skin through skinchanger:loadSkin, freezes the player, moves the ped to coords, waits up to 5 seconds for collision, resurrects the ped and fires the playerSpawned event. Used by ESX on first spawn and by multicharacter resources.
| Name | Type | Description |
|---|---|---|
skin | table | Skin table for skinchanger. |
coords | table | Table with x, y, z and heading (not w). |
cb | function | Called when the spawn is done. |
Returns: nothing
ESX.SpawnPlayer leaves the player frozen. ESX unfreezes it in its own esx:playerLoaded handler. If you call it yourself later, unfreeze the ped in cb (for example with FreezeEntityPosition(PlayerPedId(), false)).
Input and keys
ESX.RegisterInput
ESX.RegisterInput(command_name, label, input_group, key, on_press, on_release)Registers a remappable key binding. It wraps xLib.addKeybind, which creates the commands +command_name and -command_name and a key mapping the player can change in the GTA settings. The callbacks do not fire while the pause menu is open.
| Name | Type | Description |
|---|---|---|
command_name | string | Unique name. ESX prefixes it with + / -. |
label | string | Text shown in the key bindings settings. |
input_group | string | Input mapper, usually keyboard. See input mapper ids. |
key | string | Default key, for example f6. |
on_press | function? | Called on key down. |
on_release | function? | Called on key up. |
Returns: a keybind object with currentKey, isPressed, disable(self, toggle), remove(self) and getCurrentKey(self).
local menuKey = ESX.RegisterInput('myjob_menu', 'Open job menu', 'keyboard', 'f6', function()
if ESX.PlayerData.job.name == 'police' then
OpenPoliceMenu()
end
end)
print('Menu key is', menuKey.currentKey)ESX.HashString
ESX.HashString(str)Despite the name, this builds a ~INPUT_...~ text token for a command mapping, so GTA can render the bound key inside help text.
| Name | Type | Description |
|---|---|---|
str | string | The mapped command name. For keys made with ESX.RegisterInput, the mapped command is + plus your name. |
Returns: string
ESX.ShowHelpNotification(('Press %s to open the menu'):format(ESX.HashString('+myjob_menu')))If the token does not render for your binding, use the currentKey field of the object returned by ESX.RegisterInput instead.
Vehicles
ESX.GetVehicleTypeClient
ESX.GetVehicleTypeClient(model)Returns the vehicle type string FiveM expects for CreateVehicleServerSetter, based on the vehicle class plus a list of known exceptions. ESX.GetVehicleType is an alias on the client.
| Name | Type | Description |
|---|---|---|
model | string | number | Model name or hash. |
Returns: string|false: one of automobile, bike, boat, heli, plane, submarine, trailer, train, or false when the model does not exist or is not a vehicle.
print(ESX.GetVehicleTypeClient('policeb')) -- bike
print(ESX.GetVehicleTypeClient('kosatka')) -- submarineThe server version is documented in Server functions.
Common issues
ESX.PlayerData.jobis nil. You read it before the player loaded. Wait forESX.IsPlayerLoaded()or theesx:playerLoadedevent.- Changes from
ESX.SetPlayerDatadisappear. The server owns the real data and pushes it again on the next update. - Inventory helpers return nothing.
ESX.SearchInventoryandESX.ShowInventoryonly work with the built-in inventory, not withConfig.CustomInventory.
Related
Frequently asked questions
Independent documentation
Last updated on
OneSync Functions
ESX.OneSync server functions for ESX Legacy. Find nearby players, peds, objects and vehicles and spawn server-owned entities with SpawnVehicle and SpawnPed.
Client UI
ESX Legacy client UI functions for notifications, help text, TextUI, progress bars, the context menu and ESX.UI.Menu, with parameters and examples.