How do i update :GetChildren?

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

Why not just use a function that fires whenever the Value of an IntValue changes?

I did, it’s just inside the function

Try using

child.GetPropertyChangedSignal('Value'):Connect(function()

end)

I dont know if this will fix it but I recommend using “PropertyChangedSignal()” instead of “Changed()”

1 Like
local function onChildAdded(child)
	if child:IsA("IntValue") then
		child.Changed:Connect(function()
			print(child.Name .. "'s value has changed")

But this only fires when a child is added first off, and then checks if that item has an IntValue child which is changed. If no child is added then a change in the IntValue doesn’t register.

I’m saying have a separate function that only checks for a change in the IntValue.Value to fire the function that adds to the IntValue and then updates it in the GUI.

sorry i was sleeping, but ill check if these solutions work

would it still work if the intValue was created by Instance.new, when it was added to the inventory?

you have to put :GetChildren inside function for update

When you create the IntValue it’s in the game in the Explorer location you placed it. If you need to update the IntValue.Value it should be updated by your script at that location.

i just gave up so ill just mark this as solution

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.