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…
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
-- setJob(name, grade)
xPlayer.setJob('police', 2) -- police, grade 2Assign job and grade
Example: a police-only command
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/3Completa para leer el nombre del trabajo del jugador.
local xPlayer = ESX.GetPlayerFromId(source)local nombre = xPlayer..namePista
El trabajo vive en xPlayer.job, con .name, .label, .grade y .grade_name.
¿Cómo le das a un jugador el trabajo de policía con rango 2?
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:
RegisterCommand('multar', function(source) local xPlayer = ESX.GetPlayerFromId(source) -- aqui la logica de multarend, 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:
