[lua] How do I get the players job server sided

Hello everyone,

Once more I seem to be stuck on something simple. I also can’t seem to find much information about it. There is only one similar topic on the forum here which is client side. And I can’t even get that to work for me.

All I need is to check the players job and put that information in a local variable.

I’ve tried:

ESX = nil

Citizen.CreateThread(function()
    while ESX == nil do
        TriggerEvent('esx:getSharedObject', function(obj) ESX = obj end)
        Citizen.Wait(0)
    end

    while ESX.GetPlayerData() == nil do
        Citizen.Wait(10)
    end

end)

local source = source
local job = ESX.GetPlayerData(source).job

Also tried:

local source = source
local xPlayer = ESX.GetPlayerFromId(source)
local job = xPlayer.job.name

Anyone that has the answer for me?

2 Likes

try this

ESX = nil

TriggerEvent('esx:getSharedObject', function(obj) ESX = obj end)

RegisterServerEvent("getJob")
AddEventHandler("getJob", function(source, cb)
     local _source = source
     local xPlayer = ESX.GetPlayerFromId(_source)
     local job = xPlayer.job.name

end)

1 Like

callback whatever you want, i usually cb true/false

I guess you code doesn’t work as it gets called before the esx:getSharedObject is even finished

Nope.

     local job = xPlayer.job.name

This line causes an error.

If you have a different way of accomplishing the same please let me know.

For some reason I can’t use the source to find the player information.

If I use a target’s information it works just fine.

    local _source = source
    local player = player
    local xPlayer = ESX.GetPlayerFromId(player)
    local job = xPlayer.job.name

    print(job)

    if source == nil then
        print("no")
    else
        print("yes")
    end

    local _player = ESX.GetPlayerFromId(_source)

    if _player == nil then
        print("f me")
    else
        print("nais")
    end

Gives:

unemployed
yes
f me

1 Like

Thank you this helped me in a script that I am currently working on :grinning: