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
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.
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
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.
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.
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.
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)