FiveM Code attempt to index a nil value (upvalue 'ESX')

Hello, I’m a German beginner developer and I have a problem.
I make a “support” command and my problem is when I type in game the “support” command one this Error come: attempt to index a nil value (upvalue ‘ESX’)
This is my code:

local ESX = nil

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

RegisterServerEvent('SupportB:callSupport')
AddEventHandler('SupportB:callSupport', function(message, location)

    local xPlayerSender = ESX.GetPlayerFromId(source)

    for k, v in pairs(GetPlayers()) do
        local xPlayerTarget = ESX.GetPlayerFromId(v)
        local targetGroup = PlayerTarget.getGroup()

        if targetGroup == 'admin' then
            TriggerClientEvent('Support:showSupport', xPlayerTarget.source, xPlayerSender.name, xPlayerSender.source, message, location)
        end
    end

end)

Can anyone help me? :face_with_monocle:

Looks ok for me… Do you got other scripts which are using ESX?
I don´t think its different but for me its working.

ESX = nil

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

RegisterNetEvent("example")
AddEventHandler("example", function()
    local xPlayer = ESX.GetPlayerFromId(source)
    print(xPlayer.identifier)
end)

Hi @Jamie32 Jamie, yes, I have more ESX code here:

ESX = nil

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

and

RegisterCommand("support", function(source, args, rawCommand)

    local message = ''
    for i=1, #args, 1 do
        message = message .. '' .. args[i]
    end

    local playerPed = PlayerPedId()
    local playerLocation = GetEntityCoords(playerPed)

    TriggerServerEvent('SupportB:callSupport', message, playerLocation)

end)

RegisterNetEvent('Support:showSupport')
AddEventHandler('Support:showSupport', function(playername, playerid, message, location)
    
    ESX.ShowNotification('~r~Supportanfrage!\n~g~' .. playername .. '~s~(~g~' .. playerid .. '~s~) braucht Hilfe! \nNachricht: ~o~' .. message)

    local blip = AddBlipForCoord(location.x, location.y)
    SetBlipSprite(blip, 280)
    SetBlipScale(blip, 1.0)
    SetBlipColour(blip, 49)
    BeginTextCommandSetBlipName("STRING");
    AddTextComponentString('Supportanfrage')
    EndTextCommandSetBlipName(blip)

    local time = 0
    while time < 800 do
        time = time + 1
        showInfobar('Drücke ~g~E~s~, um dich zu ~g~' .. playername .. ' ~s~zu teleportieren.')
        if IsControlJustReleased(0, 38) then
            SetEntityCoords(PlayerPed(), location.x, location.y, location.z + 1.0)
            time = 800
            RemoveBlip(blip)
        end

        Citizen.Wait(1)
    end
end)

function showInfobar(msg)

    CurrentActionMsg  = msg
    SetTextComponentFormat('STRING')
    AddTextComponentString(CurrentActionMsg)
    DisplayHelpTextFromStringLabel(0, 0, 1, -1)
   
end

Look out in the code is a bit german text!
Thanks for help @Jamie32 :grin:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.