Question on script connections

Roblox shows that you can use the following code to ensure the hit connection is disconnected. I was curious if you could do the same for connections in scripts? Or should you just stick to keeping track of all connections and disconnecting them when they are no longer needed? Furthermore I’m curious to know if this method could also get rid of tweens??
From my understanding the connections will be disconnected however sometimes they may not be leading to a memory leak so its better to keep track of all connections and disconnect them yourself??

	-- To ensure our .Hit connectdion gets isconnected, destroy the explosion once it's removed.
	explosion.AncestryChanged:Connect(function()
		if not explosion.Parent then
			explosion:Destroy()
		end
	end)

RBXScriptConnection (Connection instances) will automatically disconnect once the connected instance has been destroyed. This is why we destroy the explosion, once it’s been destroyed, all connections are disconnected to that explosion, and then garbage is collected.

I don’t think connections in a Script will cause those connections to be destroyed when the Script itself is destroyed. So like you said, it’s good practice to destroy all connections when necessary.

2 Likes

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