QBCore in depth: the player and their money · Lesson 4/4 · 9 min

Metadata and player identity

QBCore stores extra data (hunger, thirst, stress, handcuffed...) in metadata, and identity in citizenid and charinfo.

Besides money and job, QBCore stores character data in two key places: metadata (states like hunger/thirst/stress) and charinfo (first name, last name, date of birth...). The character's unique identity is the citizenid.

lua
local Player = QBCore.Functions.GetPlayer(source)

-- Identity
local cid = Player.PlayerData.citizenid     -- 'ABC12345' (unique per character)
local nombre = Player.PlayerData.charinfo.firstname
local apellido = Player.PlayerData.charinfo.lastname

-- Metadata (states)
local hambre = Player.PlayerData.metadata['hunger']
local sed = Player.PlayerData.metadata['thirst']

Read identity and metadata

Change metadata

lua
-- Raise hunger to 100
Player.Functions.SetMetaData('hunger', 100)

-- Mark as handcuffed
Player.Functions.SetMetaData('ishandcuffed', true)

SetMetaData

The citizenid is the key you'll use in your own database tables to link data to a character (houses, vehicles, etc.). It's stable across sessions; the source is NOT (it changes every time the player joins).

Practice what you learned

0/3
Rellena los huecos

Lee el nivel de hambre del jugador desde su metadata.

1local hambre = Player.PlayerData.['hunger']
Pista

Los estados (hambre, sed, estrés…) viven en metadata, indexados por su nombre.

Test

¿Por qué se usa el citizenid (y no el source) como clave en tus tablas de base de datos?

Corrige el error

Este comando /comer debería subir el hambre a 100, pero accede mal a la función. Corrígelo usando SetMetaData.

Este código tiene un fallo:

1local Player = QBCore.Functions.GetPlayer(source)
2if not Player then return end
3Player.PlayerData.metadata['hunger'] = 100

Reescríbelo corregido:

Pista

No asignes directamente sobre PlayerData; usa la función oficial Player.Functions.SetMetaData('hunger', 100).

Challenge: code it yourself

Make a /comer command that raises the player's hunger to 100 using metadata.

Write it yourself in your editor (VS Code) and test it on your server. You learn here by doing it, not by copying.

See hint

GetPlayer(source) → if not Player return → Player.Functions.SetMetaData('hunger', 100).

Escribe aquí tu solución:

How was this lesson?