Why does ChildAdded fire in studio, but not in-game?

I am working on an inventory system, that every time a value is added to the inventory folder, it adds it to the Gui.
Everything worked perfectly fine in the studio. It worked every time. But when I join the game, the Gui is empty, and I am sure that the items were loaded because I data stored them, and printed their names and values.

I tried to wait until the character exists, I tried to check if the Gui is empty and then clone the template to the GUI.
I also tried to put prints to see if it works and if the function runs, the prints are printed in the studio, and not in-game.
I know that the script itself runs because I also printed outside of any functions, and it printed both in-game and in the studio

That’s my local script, the parent of it is the frame, and there’s a grid layout there:

local player = game:GetService("Players").LocalPlayer 
local inventory = player:WaitForChild("Inventory")

inventory.ChildAdded:Connect(function(item) 
	if(not script.Parent:FindFirstChild(item.Name)) then
		local clone = game:GetService("ReplicatedStorage"):WaitForChild("Template"):Clone()
		clone.Name = item.Name
		clone.ItemName.Text = item.Name
		clone.Count.Text = item.Value
		
		item:GetPropertyChangedSignal("Value"):Connect(function()
			clone.Count.Text = item.Value
		end)
		clone.Parent = script.Parent
	end
end)

Does anyone know why it fires in the studio but not in-game?
Thanks for everyone who responds :slightly_smiling_face:

1 Like

Studio and In-game aren’t entirely accurate. The most likely cause is that the localscript initializes after everything is placed into the inventory. You should use a for loop that runs only once and then use ChildAdded to ensure that any stuff gets added later on. Just my guess.

1 Like

Thanks :slightly_smiling_face:, I’ll try to do that again, maybe I did something wrong.
I tried to do such a thing too, but it was no success before.