Frameworks: ESX and QBCore · Lesson 4/5 · 9 min

Jobs with ESX

Each player has a job and a grade. Learn to read them and to assign a new job.

The player's job lives in xPlayer.job. It's the foundation of almost any roleplay system: police, EMS, mechanic, gangs…

lua
local xPlayer = ESX.GetPlayerFromId(source)
local nombre = xPlayer.job.name    -- 'police'
local etiqueta = xPlayer.job.label -- 'Police'
local rango = xPlayer.job.grade    -- 0, 1, 2…
local rangoNombre = xPlayer.job.grade_name -- 'cadet'

Read the job

Assign a job

lua
-- setJob(name, grade)
xPlayer.setJob('police', 2) -- police, grade 2

Assign job and grade

Example: a police-only command

lua
RegisterCommand('multar', function(source)
  local xPlayer = ESX.GetPlayerFromId(source)
  if not xPlayer then return end
  if xPlayer.job.name ~= 'police' then
    return TriggerClientEvent('chat:addMessage', source, { args = { 'Sistema', 'You are not a police officer.' } })
  end
  -- the fining logic here…
end, false)

Restrict by job

Job names ('police', 'ambulance', 'mechanic') are in the jobs table of the database. ALWAYS check the job on the server before granting access to anything.

Practice what you learned

0/3
Rellena los huecos

Completa para leer el nombre del trabajo del jugador.

1local xPlayer = ESX.GetPlayerFromId(source)
2local nombre = xPlayer..name
Pista

El trabajo vive en xPlayer.job, con .name, .label, .grade y .grade_name.

Test

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

Corrige el error

Este comando debería ser solo para policías pero no comprueba el trabajo. Añade que solo continúe si xPlayer.job.name es 'police'.

Este código tiene un fallo:

1RegisterCommand('multar', function(source)
2 local xPlayer = ESX.GetPlayerFromId(source)
3 -- aqui la logica de multar
4end, false)

Reescríbelo corregido:

Pista

Compara xPlayer.job.name con 'police' y haz return si no coincide.

Challenge: code it yourself

Make a /cobrarsueldo command that only works if the player is 'mechanic' and pays them based on their grade (grade × 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 xPlayer.job.grade and multiply it; validate xPlayer.job.name first.

Escribe aquí tu solución:

How was this lesson?