luafivemesx

How can I integrate police job to this command?


I couldn't make only "police" jobs can toggle this command with if xPlayer.job.name == "police" then this code but it didn't worked as I planed

local xPlayers = ESX.GetPlayers()

RegisterNetEvent("pepperspray:Togglepepperspray")
AddEventHandler("pepperspray:Togglepepperspray", function()

    if not holdingpepperspray then
        RequestModel(GetHashKey(peppersprayModel))
        while not HasModelLoaded(GetHashKey(peppersprayModel)) do
            Citizen.Wait(100)
        end

        RequestAnimDict(animDict)
        while not HasAnimDictLoaded(animDict) do
            Citizen.Wait(100)
        end
    end

Solution

  • you can do like this :

    RegisterNetEvent("pepperspray:Togglepepperspray")
    AddEventHandler("pepperspray:Togglepepperspray", function()
    
        local xPlayer = ESX.GetPlayerData();
    
        if xPlayer.job.name == "police" then
    
            if not holdingpepperspray then
                RequestModel(GetHashKey(peppersprayModel))
                while not HasModelLoaded(GetHashKey(peppersprayModel)) do
                    Citizen.Wait(100)
                end
    
                RequestAnimDict(animDict)
                while not HasAnimDictLoaded(animDict) do
                    Citizen.Wait(100)
                end
            end
        else
            print("Vous n'avez pas le bon metier");
        end
    
    end)
    

    Dont use your first line of your script, because is just use during the script load. For efficiency, use it only when you need on a function and before a for or while.