Table getting nil help!

So I really confused right now. I have a module that adds items to the inventory but there is an issue, whenever I check the quantity the output says nil. I tried checking this with other stats like health and they print 0. I use quantity to check if they own it as well as how much of that item they have.

give.Give = function(tool, tablearea, player, equiptable) -- Tool being weapon/armour/potions / Tablearea being Weapons.WeaponTable

	for num, item in pairs(ReplicatedStorage[tool]:GetChildren()) do  -- Checks all the weapons in the weapons folder
		for serial, name in pairs(tablearea) do -- Checks table
			print(tablearea[serial].Quantity) -- test it prints out nil
			if item.Name == tablearea[serial].Name and tablearea[serial].Quantity > 0 then -- if they own it // The value in quantity is 0
				print(item)
				if item.Stats.Serial.Value == serial then -- Checks to see if it's the correct serial
		            for ser, itm in pairs(equiptable) do
						if serial == equiptable[tool] then
							print("You made it here!")
							local humanoid = player.Character:FindFirstChildOfClass("Humanoid")
							local weap = item:Clone()
							humanoid:EquipTool(weap)
						end
					end
					for i = 1,name["Quantity"] do
						--print("We made it here")
						adding.AddedEvent(item, tool, player)
					end
				end
			end
		end
	end
end

This is the table for the weapons:

local Weapons = {}

Weapons.WeaponsTable = {
	
	[1] = {
		["Name"] = "Wooden Sword",
		["Damage"] = 5,
		["Magic Damage"] = 2,
		["Health"] = 0, 
		["Rarity"] = "Common",
		["Quantity"] = 5
	},
	
	[2] = {
		["Name"] = "Wooden Sword",
		["Damage"] = 7,
		["Magic Damage"] = 4,
		["Health"] = 0, 
		["Rarity"] = "Common",
		["Quantity"] = 0
	},
	
	[3] = {
		["Name"] = "Wooden Sword",
		["Damage"] = 10,
		["Magic Damage"] = 5,
		["Health"] = 0, 
		["Rarity"] = "Common",
		["Quantity"] = 0
	},
	
	[4] = {
		["Name"] = "Wooden Sword",
		["Damage"] = 13,
		["Magic Damage"] = 7,
		["Health"] = 2, 
		["Rarity"] = "Common",
		["Quantity"] = 0
	},
	
	[5] = {
		["Name"] = "Stone Sword",
		["Damage"] = 5,
		["Magic Damage"] = 2,
		["Health"] = 0, 
		["Rarity"] = "Common",
		["Quantity"] = 0
	},
	-- [2].. [3].. [4].. and so on.
	
}



return Weapons

So if anyone can tell me why it prints nil, that would be really helpful. Thank you!

Edit:
If i use print(tablearea[serial].Quantity) it will print nil.
If i use print(tablearea[serial].Health) it will print 0.

So why does Quantity output nil.

If there are any replies, I may not be able to see them as I need to sleep. As soon as I am back on I will reply and try any suggestions that are posted. Thank you.

1 Like

If you were passing the table you show as the tablearea parameter to your function, it would not be printing nil. So something is going wrong outside of the code you’ve shown us. Try replacing your print statement with one that prints all the keys and values in the table, e.g.

for i,itemData in pairs(tablearea) do
    print("Data for serial number:",i)
    for k,v in pairs(itemData) do
        print(k,"=",v)
    end
    print("") -- blank line
end
2 Likes

Sorry for the really late response. I have tried this and it shows this:

which is all the tables, so if I wanted to find quantity I would do a for loop in the tablearea and then a for loop for the value of tablearea?

for num, item in pairs(ReplicatedStorage[tool]:GetChildren()) do
		for i,itemData in pairs(tablearea) do
			for k,v in pairs(itemData) do
				if item.Name == k["Name"] and k["Quantity"] > 0 then -- if they own it // The value in quantity is 0
					print("You own it")
				end      
			end
		 end
	end

So I did something like this, and it doesn’t reach the You Own It print, also it also seems to skip the first serials with the name of wooden Sword, I tried printing out k = v and It would only print out stone sword.

Edit: It doesn’t skip over wooden anymore, but it also doesn’t print You Own it. I’ve tried k.Name and k.Quantity and just v and these don’t seem to work.

Edit 2:
I believe I have solved it by doing itemData.Name and itemData.Quantity. For some reason it does it 6 times.

Try replacing it with value.Quantity

Attempt to index number with Quantity. I assume you meant this: v.Quantity.

Edit:
I spend a while on this now and I can’t seem to find out why it does 6, I changed the datastoreStore to a different master key, that didn’t work, I tried making it so if itemData.Quantity >= 1 then and that didn’t seem to work.

Current code for giving the items, which is where it duplicates 6 times for some reason.

give.Give = function(tool, tablearea, player, equiptable) -- Tool being weapon/armour/potions / Tablearea being Weapons.WeaponTable
	
	for num, item in pairs(ReplicatedStorage[tool]:GetChildren()) do
		print(item:WaitForChild("Stats").Serial.Value)
		for i,itemData in pairs(tablearea) do
			for k,v in pairs(itemData) do
				--print(v.Quantity)
				if item.Stats.Serial.Value == i and itemData.Quantity >= 1 then -- if they own it 
					 for ser, itm in pairs(equiptable) do
						if itemData.Serial == equiptable[tool] then
							print("You made it here!")
							local humanoid = player.Character:FindFirstChildOfClass("Humanoid")
							local weap = item:Clone()
							humanoid:EquipTool(weap)
						end
					end
					for i = 1,itemData.Quantity do
						--print("We made it here")
						adding.AddedEvent(item, tool, player)
					end
				end      
			end
		 end
	end

So why you were getting Quantity nil, your original problem, is clear from looking at the output of the debug code I gave you. Your weapons table’s entries [1] and [2] don’t actually have “Quantity” as a key in item subtables. In the example WeaponsTable you showed us, every entry had Quantity as a key. This is also why your code was skipping the first two Wooden Swords.

Your code that never reached “You own it” had a different problem. You had k["Name'] and k["Quantity"] in your conditional, but k an integer index (your item’s serial number), not a table.

The last issue, of why your revised code prints 6 times… it’s because you’ve still got the for k,v in pairs(tablearea) do loop in your code. I only suggested that for debug output (to print all the key value pairs in the table), you don’t want that loop in your final code, it’s causing you to run the check you’re trying to do redundantly, once for every key in the itemData table! Just delete that for k,v loop and the matching end.

1 Like

Thank you, I really appreciate the help and explanations.

1 Like

No problem, glad I could help get you unstuck!

1 Like