Jobs, gangs and items · Lesson 1/3 · 9 min
Jobs and grades with QBCore
Each player has a job, a grade and a duty status (onduty). Learn to read them and assign them.
The player's job lives in Player.PlayerData.job. It's the foundation of almost any roleplay system: police, EMS, mechanic, gangs...
local Player = QBCore.Functions.GetPlayer(source)
local job = Player.PlayerData.job
local nombre = job.name -- 'police'
local etiqueta = job.label -- 'Police'
local rango = job.grade.level -- 0, 1, 2...
local rangoNombre = job.grade.name -- 'cadet'
local enServicio = job.onduty -- true / falseRead the job
Give a job
-- SetJob(name, grade)
Player.Functions.SetJob('police', 2) -- police, grade 2Assign job and grade
Example: command only for on-duty police
RegisterCommand('multar', function(source)
local Player = QBCore.Functions.GetPlayer(source)
if not Player then return end
local job = Player.PlayerData.job
if job.name ~= 'police' or not job.onduty then
return QBCore.Functions.Notify(source, 'You are not on duty as police.', 'error')
end
-- the fining logic goes here...
end, false)Restrict by job and duty
Jobs are defined in qb-core/shared/jobs.lua (each with its grades). ALWAYS check the job on the SERVER before granting access to anything; the client can lie.
Practice what you learned
0/3Completa para leer el nombre del trabajo del jugador en el servidor.
local Player = QBCore.Functions.GetPlayer(source)local nombre = Player.PlayerData..namePista
En QBCore los datos del jugador cuelgan de Player.PlayerData; el trabajo es uno de ellos.
¿Cómo le asignas a un jugador el trabajo de policía con rango 2?
Este comando /multar debería ser solo para policías, pero no comprueba nada. Añade que solo continúe si el trabajo es 'police'.
Este código tiene un fallo:
RegisterCommand('multar', function(source) local Player = QBCore.Functions.GetPlayer(source) if not Player then return end -- aqui la logica de multarend, false)Reescríbelo corregido:
Pista
Compara Player.PlayerData.job.name con 'police' y haz return si no coincide. Esto va SIEMPRE en el servidor.
Challenge: code it yourself
Make a /cobrarsueldo command that only works if the player is a 'mechanic' and pays them according to their grade (grade.level × 100).
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
Read job.grade.level and multiply it; validate job.name == 'mechanic' first.
Escribe aquí tu solución:
