Allow a resource only for a certain job (ESX server)

Hi guys,

New here and new to the coding community.

I have found a nice Tablet that I want to use as a CAD station for the Ambulance guys in the server.

The script itself has only 1 lua file and all I’m trying to do is to make it so it allows the tablet to be opened / closed by player with job == ambulance in the database.

I tried looking at the esx_ambulancejob and esx_policejob scripts to figure out how they make sure only EMS can open EMS menus / likewise for police, but all I found were something along the following lines which don’t work for the script I’m trying to customize a bit.

The parts I added are the lines with ++ at the beginning

++ ESX = nil

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

Citizen.CreateThread(
    function()
     -- Wait for nui to load or just timeout

		local l = 0
        local timeout = false
        while not tabLoaded do
            Citizen.Wait(0)
            l = l + 1
            if l > 500 then
                tabLoaded = true --
                timeout = true
            end
        end

        if timeout == true then
            print("Failed to load tablet nui...")
        -- return ---- Quit
        end

        print("::The client lua for tablet loaded::")

        REQUEST_NUI_FOCUS(false) -- This is just in case the resources restarted whilst the NUI is focused.

++		local xPlayer = ESX.GetPlayerFromId(source)**

++		if xPlayer.job.name == 'ambulance' then**  

			while true do
				-- Control ID 20 is the 'Z' key by default
			   -- 244 = M
				-- Use https://wiki.fivem.net/wiki/Controls to find a different key
				if (IsControlJustPressed(0, 244)) and GetLastInputMethod( 0 ) then
					tabEnabled = not tabEnabled -- Toggle tablet visible state
					REQUEST_NUI_FOCUS(tabEnabled)
					print("The tablet state is: " .. tostring(tabEnabled))
					Citizen.Wait(0)
				end
				if (tabEnabled) then
					local ped = GetPlayerPed(-1)
					DisableControlAction(0, 1, tabEnabled) -- LookLeftRight
					DisableControlAction(0, 2, tabEnabled) -- LookUpDown
					DisableControlAction(0, 24, tabEnabled) -- Attack
					DisablePlayerFiring(ped, tabEnabled) -- Disable weapon firing
					DisableControlAction(0, 142, tabEnabled) -- MeleeAttackAlternate
					DisableControlAction(0, 106, tabEnabled) -- VehicleMouseControlOverride
				end
				Citizen.Wait(0)
			end
++		end
    end
	
)

Thanks if you have any lead to help me make this work

I could be wrong, but I think you’re trying to use a server-sided approach to determine the job. Try something like this, which gets the job once at the beginning and then just listens for job change events after that. (following your ++ = changed/new line style, except for the new functions)

++ ESX = nil
++ local playerJobName = nil

Citizen.CreateThread(
    function()
        ++ loadEsx()
        ++ initializeJob()
        ++ registerJobChangeListener()

        -- Wait for nui to load or just timeout

        local l = 0
        local timeout = false
        while not tabLoaded do
            Citizen.Wait(0)
            l = l + 1
            if l > 500 then
                tabLoaded = true
                timeout = true
            end
        end

        if timeout == true then
            print("Failed to load tablet nui...")
        -- return ---- Quit
        end

        print("::The client lua for tablet loaded::")

        REQUEST_NUI_FOCUS(false) -- This is just in case the resources restarted whilst the NUI is focused.

        while true do
++            if playerJobName == 'ambulance' then**  
                -- Control ID 20 is the 'Z' key by default
                -- 244 = M
                -- Use https://wiki.fivem.net/wiki/Controls to find a different key
                if (IsControlJustPressed(0, 244)) and GetLastInputMethod( 0 ) then
                    tabEnabled = not tabEnabled -- Toggle tablet visible state
                    REQUEST_NUI_FOCUS(tabEnabled)
                    print("The tablet state is: " .. tostring(tabEnabled))
                    Citizen.Wait(0)
                end
                if (tabEnabled) then
                    local ped = GetPlayerPed(-1)
                    DisableControlAction(0, 1, tabEnabled) -- LookLeftRight
                    DisableControlAction(0, 2, tabEnabled) -- LookUpDown
                    DisableControlAction(0, 24, tabEnabled) -- Attack
                    DisablePlayerFiring(ped, tabEnabled) -- Disable weapon firing
                    DisableControlAction(0, 142, tabEnabled) -- MeleeAttackAlternate
                    DisableControlAction(0, 106, tabEnabled) -- VehicleMouseControlOverride
                end
                Citizen.Wait(0)
++            else
++                Citizen.Wait(1000)
++            end
        end
    end
)

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

function initializeJob()
    while true do
        local playerData = ESX.GetPlayerData()
        if playerData.job ~= nil then
            handleJobChange(playerData.job)
            break
        end
        Citizen.Wait(10)
    end
end

function registerJobChangeListener()
    RegisterNetEvent('esx:setJob')
    AddEventHandler('esx:setJob', handleJobChange)
end

function handleJobChange(job)
    playerJobName = job.name
end
4 Likes

I will test it today! Seems promising thank you

This is actually working like a charm ! Thank you so much, it got me to learn something new !!

1 Like

@JeanClode I tried it for the policejob and it dosen’t work for me, is it still working for you ?

What does the then** mean i dont get it. Its giving me a fail. I assume its because i have to code the rest myself but im not sure

1 Like