I’ve found that many of us have this error : attempt to index a nil value (local ‘xPlayer’) for many scripts.
ISSUE:
When the player is inside a settimeout loop (means that he is harversting/doing treatments/or selling) like this one :
local xPlayer = ESX.GetPlayerFromId(_source)
local itemQuantity = xPlayer.getInventoryItem('item').count
SetTimeout(5000, function()
xPlayer.removeInventoryItem('item', 1)
Transform(source, zone)
end)
If the player crash when inside this 5s loop, the xPlayer.removeInventoryItem isn’t satisfied because the player dropped. This can cause a server crash or a general timeout.
if xPlayer ~= nil then
-- do whatever threw the error before
end
EXAMPLE FIX:
local xPlayer = ESX.GetPlayerFromId(_source)
if xPlayer ~= nil then
local itemQuantity = xPlayer.getInventoryItem('item').count
SetTimeout(5000, function()
xPlayer.removeInventoryItem('item', 1)
Transform(source, zone)
end)
end
REMARK
You have to implement this fix in each script (server side) that have harverst/treamtment/selling functions with xPlayer calls
local xPlayer = ESX.GetPlayerFromId(_source)
if xPlayer ~= nil then
local itemQuantity = xPlayer.getInventoryItem('item').count
SetTimeout(5000, function()
xPlayer.removeInventoryItem('item', 2)
Transform(source, zone)
end)
end
Does this have the same effect ?
local xPlayer = ESX.GetPlayerFromId(_source)
local itemQuantity = xPlayer.getInventoryItem('item').count
SetTimeout(5000, function()
if xPlayer ~= nil then
xPlayer.removeInventoryItem('item', 2)
Transform(source, zone)
end
end)
I’m testing it and it seems to work ! Thank you @SaltyGrandpa , I’m suprised that this simple fix wasn’t shared. I may edit the title because I think your fix will help a lot of people.
It’s probably worth noting that the error isn’t a huge deal. The script will more than likely continue to function normally. I just like a nice, clean server console so I know if and when something does go wrong.
if Config.EnableESXIdentity then
local xPlayer = ESX.GetPlayerFromId(source)
local result = MySQL.Sync.fetchAll('SELECT firstname, lastname FROM users WHERE identifier = @identifier', {
['@identifier'] = xPlayer.identifier
})
local firstname = result[1].firstname
local lastname = result[1].lastname
local data = {
firstname = firstname,
lastname = lastname,
}
end