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.
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.
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.
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.
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:
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.
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.
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
I changed the study icons, the module in this example must be a child of the LocalScript.