Why is using "for i,v in pairs(table)" not working

  1. What do you want to achieve? Keep it simple and clear!
    What i’m trying to do is make an inventory system with a limit. Basically I made a table with different items, and if the item has a value of 1 or greater, a UI is made for the item.

  2. What is the issue? Include screenshots / videos if possible!
    The issue is that when i’m trying to grab the variables from the table, the script could never detect if the variable is equal to one or above.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I tried using “for i,v pairs(table) do” but it doesn’t work.

Script:

local img = game.ReplicatedStorage.Image
local inv = script.Parent.Parent.Parent.Parent.Parent:WaitForChild("Inventory")

local clonedimg = img:Clone()

local ImageId = "IMAGE ID HERE"

local module = require(inv.InventoryStoredValues)

local maxslot = 5
local item = module.Metal

script.Parent.MouseButton1Click:Connect(function()
	local ItemCount = 0
	for i,v in pairs(module) do
		if v > 0 then --This is where it doesn't work.
			ItemCount = ItemCount + 1
		end
	end


	if item == 0 then
		item = item + 1
		clonedimg.Parent = inv.Images
		clonedimg.Name = script.Parent.Name
		clonedimg.Image = "https://www.roblox.com/asset-thumbnail/image?assetId="..ImageId.."&width=420&height=420&format=png"
		clonedimg.Quantity.Text = "x"..item

	elseif item > 0 then
		item = item + 1
		clonedimg.Quantity.Text = "x"..item
	end
end)

ModuleScript:

local inventoryItems = {
	Metal = 0;
}

return inventoryItems

Basically, the problem is, the variable “ItemCount” won’t increase because the “if v > 0 then” won’t work.

2 Likes

Try changing the variable name “module” to something else. Like ModuleScript. as I think it may be interpreting it as the global module instead of your variable.

use this in the module script instead;

local inventoryItems = {}

inventoryItems.Metal = 0

return inventoryItems

this has nothing to do with it :confused:

That’s because Metal has a value of 0, therefore it will never show. Change the value of Metal to 1 or above.

The metal value gets added 1 afterwards, but even after it gets added 1, it still doesn’t print. It’s the “if v > 0 then” that doesn’t seem to work.

You’re also caching the item (Metal in this case). If you print out item every time you clock it would always be 0. Reset the variable after every click:

script.Parent.MouseButton1Click:Connect(function()
    item = module.Metal
end)

In the require the table is in the ()
That is i think
Try this:

local module = require(inv).InventoryStoredValues

I tried that, but then neither the metal or the ItemCount goes up.

The “inv” is only a path to a folder with the module script inside that folder. When I tried the parentheses, it makes an invalid argument.

1 Like

I’m pretty sure that’s the same thing as putting it in the parentheses but I tried it anyways and it didn’t work, same results.

If the idea of this is to change the value of the module, it won’t work, you just add 1 to the variable, if the value should change, you should do it differently.

That’s what im trying to do, adding 1 to the variable. The problem is that it can’t detect if the variables are greater than one in the module.

I could be wrong here, but doesn’t it have to be #(module)?

for _, v in pairs(#module) do
      if v > 0 then
              ItemCount +=1
       end
end
  • I typed this code on mobile, it could be a little incorrect.

Nope, I tried, it says table expected, got number.

Here is an alternative:

local Module = {
	Module.InventoryItems = {
         Metal = 0

}

return Module

Then:

for i,v in pairs(module.InventoryItems) do
		if v > 0 then --This is where it doesn't work.
			ItemCount = ItemCount + 1
		end
	end

If that doesn’t work for some reason, try troubleshooting the problem by doing this.

for i,v in pairs(module) do
		if v > 0 then --This is where it doesn't work.
            print("Item") -- Use this to see if the for loop is working at least.
			ItemCount = ItemCount + 1
		end
	end

But I do have to say that he probably correctly explained the issue.

Yup, it doesn’t print for some reason.

Metal > 0 is 0 > 0
This condition will return false.

0 is not bigger than 0, therefore, the condition skips the print and the rest of the code underneath.

According to TOP_Crundee123, something should happen when you put 1 in the Metal variable.

But it should still be ok anyway. If you have 0 Metal then that means you have no items in your inventory, therefore, it should be 0 unless you add 1 to Metal inside your module.

So, for this you need to make a system and this is the most basic

Storage module
local M, Storage = {}, {}
function M.Set(Name:string, Value:number)
	Storage[Name] = Value
end
function M.Get(Name:string)
	return Storage[Name]
end

return M

this will save a value with a unique key.


Applying it to your script

LocalScript
local img = game:GetService("ReplicatedStorage").Image
local inv = script.Parent.Parent.Parent.Parent.Parent:WaitForChild("Inventory")
local Module = require(script.Storage)

local clonedimg = img:Clone()
local ImageId = "IMAGE ID HERE"
local module = require(inv.InventoryStoredValues)
local maxslot = 5

Module.Set("Metal", 0)
script.Parent.MouseButton1Click:Connect(function()
	local ItemCount = 0
	for i,v in pairs(module) do
		if v > 0 then --This is where it doesn't work.
			ItemCount = ItemCount + 1
		end
	end

	local item = Module.Get("Metal")
	if item == 0 then
		item += 1
		clonedimg.Parent = inv.Images
		clonedimg.Name = script.Parent.Name
		clonedimg.Image = "https://www.roblox.com/asset-thumbnail/image?assetId="..ImageId.."&width=420&height=420&format=png"
		clonedimg.Quantity.Text = "x".. item

	elseif item > 0 then
		item += 1
		clonedimg.Quantity.Text = "x".. item
	end
	Module.Set("Metal", item)
end)

Module.Set("Metal", 0) will define the variable and it can be done from any LocalScript, from the player itself, or from the server script

Location

image
I changed the study icons, the module in this example must be a child of the LocalScript.

Ah, alright, i’ll study that code and try to understand it.