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...

lua
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 / false

Read the job

Give a job

lua
-- SetJob(name, grade)
Player.Functions.SetJob('police', 2) -- police, grade 2

Assign job and grade

Example: command only for on-duty police

lua
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/3
Rellena los huecos

Completa para leer el nombre del trabajo del jugador en el servidor.

1local Player = QBCore.Functions.GetPlayer(source)
2local nombre = Player.PlayerData..name
Pista

En QBCore los datos del jugador cuelgan de Player.PlayerData; el trabajo es uno de ellos.

Test

¿Cómo le asignas a un jugador el trabajo de policía con rango 2?

Corrige el error

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:

1RegisterCommand('multar', function(source)
2 local Player = QBCore.Functions.GetPlayer(source)
3 if not Player then return end
4 -- aqui la logica de multar
5end, 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:

How was this lesson?