PlayerData
What ESX Legacy stores about a character and where to read it. Every field of ESX.PlayerData on the client and the xPlayer object on the server, with types.
PlayerData is the information ESX keeps about the loaded character: identity, accounts, inventory, loadout, job, group and metadata. On the server it lives on the xPlayer object. On the client a copy is stored in ESX.PlayerData and kept up to date by ESX events. Read it on the client for UI and checks, but make every decision that matters (money, items, permissions) on the server.
Overview
| Fact | Value |
|---|---|
| Server | ESX.GetPlayerFromId(source) returns the xPlayer |
| Client | ESX.PlayerData (needs @es_extended/imports.lua) or ESX.GetPlayerData() |
| Ready flag | ESX.PlayerLoaded (client) |
| Sent to client | In esx:playerLoaded, then updated by events like esx:setJob and esx:setAccountMoney |
| Saved columns | accounts, job, job_grade, group, position, inventory, loadout, metadata in users |
Getting PlayerData
shared_script "@es_extended/imports.lua"CreateThread(function()
while not ESX.PlayerLoaded do
Wait(250)
end
print("Job: " .. ESX.PlayerData.job.label)
end)
RegisterNetEvent("esx:setJob", function(job, lastJob)
print(("Job changed from %s to %s"):format(lastJob.name, job.name))
end)With the imports file, ESX.PlayerData is refreshed automatically in your resource. Without it, call ESX.GetPlayerData() each time you need fresh data, because the ESX object you got from the export is a copy.
Field reference
"Client" means ESX.PlayerData.<field>, "Server" means the xPlayer object.
| Field | Type | Client | Server | Description |
|---|---|---|---|---|
identifier | string | Yes | getIdentifier() | Character identifier, char1:... with multicharacter. |
ssn | string | Yes | getSSN() | Social security number XXX-XX-XXXX. |
name | string | Yes | getName() | Character name, or the FiveM name without an identity. |
firstName | string | Yes | get("firstName") | Default "John" until an identity exists. |
lastName | string | Yes | get("lastName") | Default "Doe". |
dateofbirth | string | Yes | get("dateofbirth") | As stored by esx_identity, default "01/01/2000". |
sex | string | Yes | get("sex") | "m" or "f". Only set when the character has an identity. |
height | number | Yes | get("height") | Height in cm, default 120. |
group | string | Yes | getGroup() | Permission group. |
job | table | Yes | getJob() | See job. |
accounts | table[] | Yes | getAccounts() | See accounts. |
money | number | Yes (snapshot) | getMoney() | Cash at load time. Not updated on the client afterwards. |
inventory | table[] | Yes | getInventory() | See inventory. |
weight | number | Yes (load time) | getWeight() | Carried weight. |
maxWeight | number | Yes | getMaxWeight() | Weight limit. |
loadout | table[] | Yes | getLoadout() | See loadout. |
metadata | table | Yes | getMeta() | Persistent custom data. |
variables | table | Yes | get(key) | Non-persistent custom data set with xPlayer.set. |
coords | vector3 | Yes (live) | getCoords() | On the client this reads the ped's current position. |
skin | table | Yes | no | Saved skinchanger data. |
dead | boolean | Yes | no | Updated on death and respawn. |
ped | number | Yes | no | Current ped handle. |
weapon | number | Yes | no | Hash of the weapon in hand. |
vehicle | number | Yes | no | Current vehicle handle, if any. |
seat | number | Yes | no | Current seat index, if in a vehicle. |
source / playerId | number | no | Yes | Server ID. Use GetPlayerServerId(PlayerId()) on the client. |
license | string | no | Yes | Identifier with type prefix. |
admin | boolean | no | Yes | Admin state at load. |
Details
job
{
name = "police",
label = "LSPD",
type = "leo",
onDuty = true,
grade = 2,
grade_name = "sergeant",
grade_label = "Sergeant",
grade_salary = 900,
skin_male = {},
skin_female = {},
}grade and grade_salary are numbers. onDuty comes from metadata.jobDuty. The field list is described in xPlayer.getJob.
accounts
An array, not a map:
{
{ name = "bank", money = 50000, label = "Bank", round = true, index = 1 },
{ name = "money", money = 500, label = "Cash", round = true, index = 2 },
{ name = "black_money", money = 0, label = "Dirty Money", round = true, index = 3 },
}The order follows Config.Accounts and is not guaranteed. Look accounts up by name:
local bank = ESX.GetAccount("bank")
print("Bank: $" .. (bank and bank.money or 0))inventory
With the default inventory: an array of the items the player owns (count above 0), sorted by label.
{
{ name = "bread", label = "Bread", count = 3, weight = 1, usable = true, rare = false, canRemove = true },
{ name = "water", label = "Water", count = 1, weight = 1, usable = true, rare = false, canRemove = true },
}local breadCount = ESX.SearchInventory("bread", true) -- 0 when the player has noneWith ox_inventory, use ox_inventory's own exports to read items.
loadout
{
{ name = "WEAPON_PISTOL", label = "Pistol", ammo = 48, components = { "clip_default", "flashlight" }, tintIndex = 0 },
}Empty with ox_inventory.
metadata
A free-form table that is saved to the database. ESX itself uses health, armor, lastPlaytime and jobDuty. Write it on the server with xPlayer.setMeta; the client copy updates automatically.
-- client
local stress = ESX.PlayerData.metadata.stress or 0Reacting to changes on the client
| Event | Updates |
|---|---|
esx:playerLoaded | Whole ESX.PlayerData. |
esx:setJob | job |
esx:setAccountMoney | One entry in accounts |
esx:addInventoryItem, esx:removeInventoryItem, esx:setInventory | inventory |
esx:setMaxWeight | maxWeight |
esx:setGroup | group |
esx:updatePlayerData | metadata or variables |
esx:onPlayerLogout | Clears ESX.PlayerData and sets ESX.PlayerLoaded = false |
With @es_extended/imports.lua you can also define a global OnPlayerData function. It is called with key, value, lastValue whenever es_extended changes a field:
function OnPlayerData(key, value)
if key == "job" then
updateJobBlips(value.name)
end
endSee client events for the full list.
Common issues
attempt to index a nil value (field 'job'): you readESX.PlayerDatabeforeesx:playerLoaded. Wait forESX.PlayerLoaded.- Stale data in a resource without imports: the exported
ESXobject is a snapshot. UseESX.GetPlayerData()or switch to@es_extended/imports.lua. xPlayer.firstNameis nil: on the server, identity fields are variables. UsexPlayer.get("firstName").- Inventory indexed by name returns nil:
inventoryis an array. UseESX.SearchInventoryorxPlayer.getInventoryItem.
Related
Frequently asked questions
Independent documentation
Last updated on
es_extended
es_extended is the core resource of ESX Legacy. Learn how to get the ESX object, how the resource is structured and how a player loads, with links to every API.
Commands
All built-in ESX Legacy chat and console commands with arguments, aliases and default permissions, plus how to change who can use them and add your own.