Player Data
Learn how to access and modify a player's data
Introduction
The Player Data object in qb-core
stores all information related to a player's character, such as personal details, job, gang affiliation, metadata, and more. These values are initialized using default values defined in the config.lua
file of qb-core
.
This guide will provide an overview of the structure, default values, and how you can access or modify player data
Configuration
The player data default values can be modified in the config.lua
file of qb-core
. These defaults are stored in QBConfig.Player.PlayerDefaults
and determine what a player starts with when creating a new character
Player Data Object Structure
As of qb-core
version 1.3.0 (check your fxmanifest.lua) you don't have to import the core to have access to the player data. You can call it like this!
exports['qb-core']:GetPlayer(source)
โ server
exports['qb-core']:GetPlayerData()
โ client
When accessing the player object on the server using QBCore.Functions.GetPlayer
pass the source (player net id) โ shown in examples
When accessing the player object on the client, you don't have to pass anything and can just call QBCore.Functions.GetPlayerData
If you prefer to access the player data directly instead of calling the functions, you can do so with this format QBCore.Players[source].PlayerData
but it's not recommended
Below is a breakdown of the player data structure, its properties, default values and how to access them when retrieving the player object
Identification Table
citizenid
string
Generated via CreateCitizenId
A unique identifier for the player.
cid
number
1
Character ID (used for multi-character systems).
Money Table
money
table
{ cash = 0, bank = 0 }
Contains the playerโs cash and bank balances.
Character Information Table
charinfo
table
{ firstname, lastname, ... }
Basic personal information about the player.
Subfields:
firstname
string
"Firstname"
The player's first name.
lastname
string
"Lastname"
The player's last name.
birthdate
string
"00-00-0000"
The player's date of birth.
gender
number
0
The player's gender (0 for male, 1 for female).
nationality
string
"USA"
The player's nationality.
phone
string
Generated via CreatePhoneNumber
The player's phone number.
account
string
Generated via CreateAccountNumber
The player's bank account number.
Job Table
job
table
{ name = 'unemployed', ... }
The playerโs current job and related information.
Subfields
name
string
"unemployed"
The job name.
label
string
"Civilian"
The display label of the job.
payment
number
10
The playerโs salary.
onduty
boolean
false
Whether the player is on duty.
isboss
boolean
false
Whether the player is the boss of the job.
grade
table
{ name = 'Freelancer', ... }
The player's job grade.
Gang Table
gang
table
{ name = 'Unaffiliated', ... }
The playerโs current gang and related information.
Subfields
name
string
"Unaffiliated"
The gang name.
label
string
"No Gang"
The display label of the gang.
isboss
boolean
false
Whether the player is the boss of the job.
grade
table
{ name = 'Unaffiliated', ... }
The player's job grade.
Metadata Table
hunger
number
100
The playerโs hunger level (0โ100).
thirst
number
100
The playerโs thirst level (0โ100).
stress
number
0
The playerโs stress level (0โ100).
isdead
boolean
false
Whether the player is dead.
inhale
boolean
false
Whether the player is handcuffed.
phone
table
{}
Stores phone-specific data such as installed apps.
Player Object Functions
Every player that is created on the server has certain functions that are attached to them. These functions can be called on them directly!
UpdatePlayerData
Update the players data on the client & server
This function triggers the event QBCore:Player:SetPlayerData
SetJob
Set a player's job and grade
job:
string
grade:
number
returns:
boolean
- true for successful, false for unsuccessful
SetGang
Set a players gang and grade
gang:
string
grade:
number
returns:
boolean
- true for successful, false for unsuccessful
Notify
Shows the player a notification
text:
string
type:
string
โ error, success, primary, warninglength (optional):
number
HasItem
Preferred to use inventory export for checking if has item! This function just routes to that
item:
string | table
amount:
number
returns:
boolean
GetName
Get's the players full character name
returns:
string
SetJobDuty
Sets the player on/off duty
This function triggers the events QBCore:Server:OnJobUpdate
and QBCore:Client:OnJobUpdate
It also triggers the function UpdatePlayerData
duty:
boolean
SetPlayerData
Set the player data value of a player specifically by it's name
This function also triggers the function UpdatePlayerData
key:
string
value:
string | table
SetMetaData
Set the metadata value of a player specifically by it's name
This function also triggers the function UpdatePlayerData
key:
string
value:
string | table | number
GetMetaData
Gets the value of a player metadata specifically by it's name
value:
string
returns:
string | table | number
AddRep
Adds rep to value to a rep specifically by it's name
This function also triggers the function UpdatePlayerData
Add different kinds of rep in qb-core/config.lua
rep:
string
amount:
number
RemoveRep
Decreases value to a rep specifically by it's name
This function also triggers the function UpdatePlayerData
Add different kinds of rep in qb-core/config.lua
rep:
string
amount:
number
GetRep
Gets the current value of rep specifically by it's name
rep:
string
returns:
number
AddMoney
Adds to the value of a specific money type by it's name
This function also triggers the function UpdatePlayerData
money:
string
returns:
boolean
RemoveMoney
Decreases the value of a specific money type by it's name
This function also triggers the function UpdatePlayerData
money:
string
returns:
boolean
SetMoney
Sets the value of a specific money type by it's name
This function also triggers the function UpdatePlayerData
money:
string
returns:
boolean
GetMoney
Returns the value of the money type supplied
money:
string
returns:
number
Save
Triggers a save of the player
Logout
Forces the player to logout back to the multicharacter screen
AddMethod
The AddMethod
function allows you to dynamically add custom methods (functions) to the player object. This is particularly useful for extending player functionality in a modular and reusable way
It's worth checking if a method already exists! You can do so by running this check
if Player.Functions.MyMethod then
print("Method already exists!")
end
method:
string
handler:
function
AddField
The AddField
function allows you to dynamically add custom fields (variables or data) to the player object. This is useful for storing additional player-specific data that isnโt part of the default PlayerData
structure
It's worth checking if a field already exists! You can do so by running this check
if Player.MyField then
print("Field already exists!")
end
field:
string
data:
any
Last updated