Hello, I tried to make a table that checks if the item value is nil. I get an error saying Attempt to index nil with number.
code
invsize = 20
local playerInventories = {}
function AddItem(plr, item, amt)
local Inventory = playerInventories[plr]
local itemType = itemModule[item]["Type"]
local inv = Inventory[itemType]
for i=1, invsize do
if inv[i][item] == nil then --error occurs here
inv[i][item] = {}
end
if table.find(inv[i][item.Name], item) then
inv[i][item.Name][amt] +=1
return
end
end
end
if i print inv[i][item] I get the same issue and if i print inv[i] I get nil.
Did you mess up somewhere? with your Inventory value did you mean to put plr.Name instead? If inv is showing up as nil then Inventory[itemType] wouldnt exist. Recheck your code for any spelling errors and stuff
Make sure to check that the values you are indexing are equal to something.
Your issue is most likely with indexing the âplayerInventoriesâ since it seems to make itâs associated variable nil.
Try making sure that plr parameter is equal to something before you index. Make sure there is a proper index set up for it as well.
if inv[i] == nil --[[check if it's nil just incase the value is false. normally you wouldn't have to compare but in this case it might be a good idea]] then
inv[i] = {}
end
wouldnât I have to do this with every thing, like i and item? because it worked for the first one and i got an error saying attempt to index nil with âitem nameâ
Oh yeah, do you have a âtemplateâ of what each item should look like? If so, you could just replace that with the empty table in my previous post.
Example:
local defaultItemData = {
Name = 'Default Item Name';
Description = 'Default Description';
}
-- Then to declare your variable:
Inventory[itemType] = Inventory[itemType] or defaultItemData
local inv = Inventory[itemType]
Ah okay, then you could just assign that as a variable.
local module = require(itemModule) -- Not sure what it's called but just replace it with wherever your module is located
local itemData = module.ItemData
And then to assign it to that table for their inventory
But from looking at your script, it appears that this would overwrite an empty inventory slot, so do you have a template for what an empty item would look like? If not, you could just check if inv[itemType] exists and if not, you could skip over that slot.
So basically, first check if item[i] exists. If not, make it a table. Then check if inv[i][item] exists, then if not, skip over it.
So:
inv[i] = inv[i] or {}
if inv[i][item] then
-- Put whatever you have to do here
end
ok, I understand. But This has caused some more issues to occur. If i pick up an item it goes to every slot and the item number doesnât change if i pick up another item. If i pick up a Different item from the same class that item wonât get added.
new code:
Inventory[itemType] = Inventory[itemType] or {}
local inv = Inventory[itemType]
for i=1, invsize do
if not inv[i] then
inv[i] = {}
if not inv[i][item] then
inv[i][item] = {}
if not inv[i][item][amt] then
inv[i][item][amt] = {}
end
end
end
if inv[i][item] then
if table.find(inv[i][item], item) then
inv[i][item][amt] +=1
break
end
end
end
Ah okay, I see. I do see some duplicate variables in here so maybe look into those as they might be causing some issues. Once you fix those, this should work. If not, itâs probably a new issue on my part.
local function createNewInventorySlot(item, amt) -- New function to create a new table, needs the item string and the amt string.
local newTable = {
[item] = {
[amt] = 1;
};
return newTable -- Return the table
end
Inventory[itemType] = Inventory[itemType] or {}
local inv = Inventory[itemType]
for i = 1, invsize do
inv[i] = inv[i] or createNewInventorySlot(item, amt)
if table.find(inv[i][item], item) --[[example of what I think is a may be a duplicate variable]] then
inv[i][item][amt] += 1
break
end
end
This didnât fix my issue unfortunately. Im still having the same issues. I could send a file If you want to look at the code and see for yourself the issues