Error with adding a value into a table

Hello, I am trying to Insert a value into a table that is inside of another table. I use a for loop to loop through the other tables and see if an object inside of the table and if it isn’t then add it to the table. The issue that im getting is the if statement to check if the item is inside of the table and I get an error saying attempt to index nil with 'object'. This script is a local script.

local function AddItemToTable(tbl)
	local plr = Players.LocalPlayer
	local T1 = Inventory.T1[plr.Name]
	local T2 = Inventory.T2[plr.Name]
	local T3 = Inventory.T3[plr.Name]
	local T4 = Inventory.T4[plr.Name]

	for i,v in pairs(tbl) do
		local itemType = ItemModule[i]["Type"]

		--Error happening here
		if i ~= T1[i] or T2[i] or T3[i] or T4[i]  then
			Inventory[itemType][plr.Name][i] = Inventory[itemType][plr.Name][i] + 1
		elseif i == T1[i] or T2[i] or T3[i] or T4[i] then
			Inventory[itemType][plr.Name][i] = 1
		end
	end
end

i prints the items name and v prints the amount. can someone please help? This is really confusing me.

1 Like

This error means that T1, T2, T3 or T4 is nil, that is, there is no table for that player. To solve it you can create a table if the player does not have one

local function AddItemToTable(tbl)
	local plr = Players.LocalPlayer
	
	if not Inventory.T1[plr.Name] then Inventory.T1[plr.Name] = {} end
	if not Inventory.T2[plr.Name] then Inventory.T2[plr.Name] = {} end
	if not Inventory.T3[plr.Name] then Inventory.T3[plr.Name] = {} end
	if not Inventory.T4[plr.Name] then Inventory.T4[plr.Name] = {} end

	local T1 = Inventory.T1[plr.Name]
	local T2 = Inventory.T2[plr.Name]
	local T3 = Inventory.T3[plr.Name]
	local T4 = Inventory.T4[plr.Name]
	
	for i,v in pairs(tbl) do
		local itemType = ItemModule[i]["Type"]

		if T1[i] or T2[i] or T3[i] or T4[i]  then
			Inventory[itemType][plr.Name][i] = Inventory[itemType][plr.Name][i] + 1
		else
			Inventory[itemType][plr.Name][i] = 1
		end
	end
end

above the function I have:

local Inventory = {
	T1 = {};
	T2 = {};
	T3 = {};
	T4 = {};
}

which I forgot to add to the code above.

What is ItemModule? If the error is at this line ItemModule[i] is nil.

I have a module that contains item data:

local module = {
	["Sword"] = {
		Name = "Sword";
		Type = "T1";
		Desc = "sword";
	};
}
return module

This does print out the type

Which line exactly is the error on?

the error occurs on line 26 but its the line with the first if statement

if i ~= T1[i] or T2[i] or T3[i] or T4[i] then

and also the lines below it as well. I just tested it.(the rest of the if statement)

This is what you did:

What you should’ve done:

if i ~= T1[i] or i ~= T2[i] or i ~= T3[i] or i ~= T4[i]  then --here
			Inventory[itemType][plr.Name][i] = Inventory[itemType][plr.Name][i] + 1
		elseif i == T1[i] or i == T2[i] or i == T3[i] or i == T4[i] then --and here
			Inventory[itemType][plr.Name][i] = 1
		end

nope. Still getting same error.
image

if x == y or z is not the same as if x == y or x == z

Im confused because if i print out i and v then i is the items name and v is the amount I picked up.

The error you got seems to be on a different line?

I commented out my current code and replaced it with yours to see if it worked.

If you read the discussion then you would understand. I forgot to reference the tables in the initial post.

local Inventory = {
	T1 = {};
	T2 = {};
	T3 = {};
	T4 = {};
}

I see. When I read it those names were different.

same thing, I just changed the variables recently

This is nil, so are the T2, T3, T4. The key plr.Name doesn’t exist but you try to index it later in the script.

I can send a file if you want to check. This is still confusing me.

This is what you did first:

This is what you did next:

Of course you’re trying to index nil by T1[i]