Attempt to perform arithmetic (add) on nil and number

Hello,
I tried to make a system that adds a item to a table depending on the items type. I get an error saying attempt to perform arithmetic (add) on nil and number Im not sure how to fix this. When i print out the table it is a nil number.

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

Inventory.T1[plr.Name] = {}
Inventory.T2[plr.Name] = {}
Inventory.T3[plr.Name] = {}
Inventory.T4[plr.Name] = {}

local T1 = Inventory.T1[plr.Name]
local T2 = Inventory.T2[plr.name]
local T3 = Inventory.T3[plr.Name]
local T4 = Inventory.T4[plr.Name]

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

1 Like

It’s erroring as there’s no number when you actually try to add one. It’s like doing nil + 1 which obviously errors. You should add a check, and if the item count is nil then set it to 0 and then add 1.

I did this but I don’t get the error but I still get a nill value. How can i fix this?

You set it to 0 like I said right?


if item ~= T1[item] or item ~= T2[item] or item ~= T3[item] or item ~= T4[item]  then
	if Inventory[itemType][plr.Name][item] == nil then
	   Inventory[itemType][plr.Name][item] = 0
	end
	Inventory[itemType][plr.Name][item] += 1
elseif item == T1[item] or item == T2[item] or item == T3[item] or item == T4[item] then
	Inventory[itemType][plr.Name][item] += 1
end

I just noticed something:
Inventory[itemType][plr.Name][item] += 1
What is “itemType”? I don’t see it defined anywhere.

itemType is a variable from a module
local itemType = ItemModule[item.Name]["Type"]

module:

local module = {
	["Sword"] = {
		Name = "Sword";
		Type = T1;
		Model = Models.Sword;
	};
}

return module

Oh, well then maybe the error is because you’re trying to find that specific item type inside of the player’s inventory, but since it’s nil then it will error.

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

You didn’t define any itemType here, so when you do this Inventory[itemType][plr.Name][item] it doesn’t find an actual “itemType” and it doesn’t work. Maybe I’m wrong but I don’t know your full script so I’m unsure on what is erroring here cause there are no mistakes I’m noticing.

T1 to T4 are the itemtypes, i just have them in a module for each item.

Oh yes sorry lol. If you try printing out the value, I guess it prints nil?

yes, I just found out that if I change Inventory[itemType][plr.Name][item] += 1 to Inventory[itemType][plr.Name][item] = Inventory[itemType][plr.Name][item] + 1 it goes to 1 but doesn’t go higher.

Add a print, see if it actually prints after the value is set to 1:

if Inventory[itemType][plr.Name][item] == nil then
   Inventory[itemType][plr.Name][item] = 0
   print("Reset to 0")
end

image
yes, this gets printed

Does it print everytime? Or just the first time?

Every time for the same item.
image

Oh, that means something is resetting the item count back to nil, since everytime it prints it means the item count is nil.

This whole code if statement is in a function and the parameters is just the item name which is item

How can i fix this? because as far as i know there is nothing that resets the code.

I’m really confused too, as there aren’t any errors. Can I see your script? I’ll try replicating the issue on a baseplate and then try to fix it from there.
(You can DM it if you prefer not to show it to everyone)

I DM’d you a file containing the issue.