I’m making an inventory system where the inventory gui changes whenever an item is added or its value is changed
I have two separate functions:
-one where when an item (which is just an IntValue representing the item) is added to your inventory folder, a frame is created for the item in inventory gui - there isnt any problem with this
-one where when the IntValue is changed, the inventory gui updates it and adds the current value - this is what i need help on
here is the code:
--VARIABLES
--getting inventory
local player = game.Players.LocalPlayer
local inventory = player:WaitForChild("Inventory")
local inventoryItems = inventory:GetChildren()
--getting gui
local playerGui = script.Parent
local menuGui = playerGui:WaitForChild("MenuGui")
local backpackGui = menuGui:WaitForChild("BackpackMenuGui")
--functions
local function AddItemGui(item)
print(item.Name .. " has been added to inventory")
local itemGui = backpackGui.BackpackInventory.Templates.ItemFrame:Clone()
itemGui.Name = item.Name
itemGui.ItemName.Text = item.Name
itemGui.ItemAmount.Text = "x"..item.Value
if item.Value > 0 then
itemGui.Visible = true
itemGui.Parent = backpackGui.BackpackInventory.ItemList
else
itemGui.Visible = false
itemGui.Parent = backpackGui.BackpackInventory.ItemList
end
end
local function onChildAdded(child)
if child:IsA("IntValue") then
child.Changed:Connect(function()
print(child.Name .. "'s value has changed")
local itemGui = backpackGui.BackpackInventory.ItemList:FindFirstChild(child.Name)
if itemGui then
itemGui.ItemAmount.Text = "x" .. child.Value
if child.Value <= 0 then
itemGui.Visible = false
else
itemGui.Visible = true
end
end
end)
end
end
--CODE
inventory.ChildAdded:Connect(AddItemGui)--this creates the gui for any new items in the inventory
--i want when item value changes, the gui updates the value
for i, itemValue in pairs(inventory:GetChildren()) do
onChildAdded(itemValue)
end
i am guessing that the the pairs loop only checks for inventory whenever the player joins and then it doesn’t check again, but im not sure