[Fixed] Attempt to call a nil value (field 'GetPlayerFromId')

Hi! I’m having issues with a script that I am doing… Basically whenever is supossed to remove the item taserclip, it appears the following error message… I don’t know what could be…

Error: attempt to call a nil value (field 'GetPlayerFromId')

Code:

local xPlayer = ESX.GetPlayerFromId(source)
xPlayer.triggerEvent('esx:removeInventoryItem', 'taserclip', 1, true)

Hey, do you have ESX set as something?

At the start of your script you need to have something similar to this:

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

I’m not sure if this is the exact code you need in your script but if you don’t have it it’s probably that

Nope, same error: attempt to call a nil value (field 'GetPlayerFromId')

In this line:

local xPlayer = ESX.GetPlayerFromId(source)

Not sure if that changes anything but i’ve seen people use

        local _source = source
        local xPlayer = ESX.GetPlayerFromId(_source)
1 Like

Not yet, same error on the same line, I don’t know why… Basically what I am trying to do is removing an item from inventory (using ESX), maybe it would be easier to understand…

Are you on server or client script side?

You should be on server side for xPlayer and your code should be

xPlayer.removeInventoryItem(item, count)

I am doing the script client sided only. I add this instead of what I had and it worked:

TriggerServerEvent("esx:removeInventoryItem", 'item_standard', 'taserclip', 1)

Thanks!

4 Likes

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