I’m receiving “table index is nil” but my script explicitly checks for the index that is nil. The script is just setting the value of that dictionary key to a number value when it doesn’t exist. What is a way to ensure that the table index is indeed nil before setting it to 0?
This script is simply to obtain the quantity of an item in the player’s inventory, which is stored in a dictionary of player inventories, storing item names and quantities.
function Inventory.GetQuantity(Player: Player, Item: string)
if Craftbag[Player.Name][Item] == nil then
Craftbag[Player.Name][Item] = 0
end
return Craftbag[Player.Name][Item]
end
Here is a picture of the code with line numbers, since line numbers are useful for seeing where the problem is:
And finally, this is the error logged in the output:
ServerScriptService.Inventory:11: table index is nil
Any clue on this will be helpful. Thanks.
Edit: One more thing, the player’s inventory index:
Inventory[Player.Name] = {}
Is connected to the PlayerAdded event, and the GetQuantity() function is binded to a Gui button, so it’s not possible for the player to trigger the GetQuantity() event before the inventory is created.
I forgot to also mention that I’m unable to replicate this on my own account, but I have a game with an active player base that shows the error in the server logs after a few hours. So in order for me to replicate this, I have to wait a few hours after I add the print statement.
The Item is also explicitly checked if it exists at all. When the player fires the RemotEvent, it sends the said string. The string is type checked and also checked if it is a valid item in the Items dictionary. So in order to GetQuantity(), the Item must be a string or it won’t work.
if Items[Item] == nil then return end
So I’m not trying to deny your idea, considering that the GetQuantity() function is called for every item that is changed or added to the inventory, which is at least 50-100 times per player. Considering there are 200+ players playing everyday, this print statement will print at least that amount, which can be a mess and it can be painstaking to visually find the Item. But we’ll give it a try, see you in a few hours.
I see. I still do think that is the issue though, there is no other explanation. I was able to replicate this issue myself:
local Inventory = {
["Player"] = {};
}
local function GetQuantity(Player, Item)
print(Player, Item)
if Inventory["Player"][Item] == nil then
Inventory["Player"][Item] = 0
end
end
GetQuantity("Player", nil)
Visual representation:
And it produces the same exact output:
11:13:01.229 Player nil - Server - Script:6
11:13:01.230 ServerScriptService.Script:9: table index is nil - Server - Script:9
11:13:01.230 Stack Begin - Studio
11:13:01.230 Script 'ServerScriptService.Script', Line 9 - function GetQuantity - Studio - Script:9
11:13:01.230 Script 'ServerScriptService.Script', Line 13 - Studio - Script:13
11:13:01.230 Stack End - Studio
I can’t think of any other explanation for your issue. I hope you do figure it out though!
That’s actually crazy, I just tested this and the error appeared. I’m not sure why this happened but I added the check to the GetQuantity and it works now.
is true. Inventory[“Player”][nil] does not exist and you’re checking if it does not exist. However, a key cannot be nil: which is why it errors when you try to assign a value to it.