Questions about connections

Hey there, I have a quick question about connections. In my game, you can bake different supplies using materials. When a player clicks on different appliances, a gui appears. I then loop through all the possible things that you can cook with that appliance, and I clone a frame for each item. Then when you click on any of those frames, it starts cooking that item. I currently have a child added event that detects when a child is added, and detects when that child is pressed. It looks like this:

script.Parent.ChildAdded:Connect(function(child)
	if child:IsA("Frame") then
		child.Select.Activated:Connect(function()
			--code here to cook that item
		end)
	end
end)

Now when they close the gui, I destroy all of the children of the frame. My question is this, when the frame is destroyed, does the connection that checks for when it is activated get disconnected? If it didn’t then if a player opened the gui 10 times, I would have 100s of connections which could cause lag. I was thinking of using the following code but I wasn’t sure if it was necessary:

script.Parent.ChildAdded:Connect(function(child)
	if child:IsA("Frame") then
		connection = child.Select.Activated:Connect(function()
			--code here to cook that item
		end)
        while wait(1) do
            if not child then
              connection:Disconnect()
               break
            end
        end
	end
end)

If this doesn’t make sense then I can explain it more cleary. Thanks for reading!

Hey XdJackyvoiiXd21!

When :Destroy() is called on an object, disconnects all connections related to that object and calls :Destroy() on the children of it.

So yes, all the connections would be disconnected by ROBLOX.

1 Like

Thank you so much, that helps a lot :smile: !

1 Like