Attempt to index nil with number

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

What if you tried:

if not inv[i][item] then
    inv[i][item] = {}
end

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.

It’s because inv[i] doesn’t exist.

You need to add another if statement:

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

The issue occurs here as well, same message attempt to index nil with number

if i print out itemType it prints out the value, there are no spelling errors that I found.

Ah okay, then Inventory[itemType] doesn’t exist. Try adding another if statement like this:

if not inv then
    Inventory[itemType] = {}
end

still same error, attempt to index nil with number

that won’t work because there are 2 equals signs

My bad, wasn’t paying attention to what I was writing. Made an edit.

You should also be able to do something like this instead, it’s much shorter:

Inventory[itemType] = Inventory[itemType] or {}
local inv = Inventory[itemType]

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]

I have a template for the item and its inside of a module

	["Apple"] = {
		Name = "Apple";
		Type = T2;
		Model = Models.Apple;
		Desc = "apple";
	};

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

shouldn’t the function createNewInventorySlot contain the data itemType, slot, item and amt instead of just item and amt?

Don’t think so. It returns the table so if inv[i] doesn’t exist, it assigns inv[i] to be that new constructed table, containing item and amt.

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