When should I disconnect functions?

Hello. Im wondering when I should disconnect functions.

For example, im making a custom leaderboard, this code will run everytime the player clicks the refresh button:

for _, child in MembersInfoFrame:GetChildren() do
    if child.Name ~= 'UIListLayout' then

for _, Player in Players:GetPlayers() do
        local TemplateClone = UIListLayout.Template:Clone()
        TemplateClone.Text = Player.Name
        TemplateClone.Parent = MembersInfoFrame

	    TemplateClone.MouseButton1Click:Connect(function() --Checking if the player clicks the the button

So the button will be getting destroyed every time the player clicks the refresh button, should I be disconnecting the function when the button(s) are destroyed? If so, how would I do it?

1 Like

Connections will automatically disconnect and should be garbage collected when the connected Instance gets destroyed.


Oh ok, thats good then. Thanks

Also what is garbage collection?

It cleans up unused memory - it is something that happens internally automatically. You would only ever need to worry about it when writing, for example, custom classes, and/or when having a reference to an Instance in a table, in which case you would have to unreference or use metamethods to make the tables weak.


Thank you

30 chaaaracters

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