Tween Completed Data leak?

If you have something like Tween.Completed:connect(function() and for some reason if never completes (bc the tween object gets deleted or say some other scenario) could it eventually cause a data leak if you have multiple of these?

Too many of anything will clog up memory. If you hold the tween in a variable which won’t get garbage collected and keep piling on more, yes.

I would assume not, unless you permanently keep track of all your tweens in some way. Once you lose all references to your tween, it should get garbage collected. When that happens the connection you made to the function should also break and get cleaned up. I’m not a full 100% sure though.

Completed will always fire. Completed signals that the tween is no longer playing. To check what happened to make the event fire, you can use the supplied argument “Enum PlaybackState”.

Tween.Completed:Connect(function (PlaybackState) print(PlaybackState) end)

You don’t have to use connect by the way.

Tween.Completed:Wait()
-- Code after

You’ll experience a memory leak if you have a variable that you don’t clean up when it’s no longer necessary. To this end, I usually wrap my tweens in a do-end block so it gets garbage collected after my tween magic is done.

-- No tabbing, on phone
do
local Tween = tweenCreateLineThing
Tween:Play()
Tween.Completed:Wait()
end -- No memory leak, variable gets GCed

You can’t call destroy on a tween anyway.

2 Likes

Yes you can, Tween inherits from Instance so you can destroy them.

Incorrect; if you destroy a tween, the tween will keep playing until it is finished, but any signals connected to the tween (Completed signals) will be disconnected, so these subsequently won’t trigger when the tween stops if it was destroyed.

5 Likes

If you’re a true connoisseur you’d know the only way to tab is by using 4 spaces

But really though, yeah. tl;dr if you’re abusing this for doing a ton of tweens, then yeah, it’ll cause issues. If it’s just a few, unless you’re really worried about small memory leaks, then it should be alright - yield functions are often better than events though.

1 Like

You’re connecting Completed after Destroying.

If you weren’t able to Destroy the tween then you’d get an error in output, which you did not.

Not sure what you mean. Destroy is Destroy and does what Destroy is meant to do.

Destroy parents it to nil (irrelevant for tweens) and destroys connections on the object. If you connect to an event of that object again afterwards, then obviously you get that print again, since the event wasnt disconnected by the Destroy that happened beforehand.

1 Like

Destroy sets the parent to nil (parent is already nil and should be) and disconnects all connections. The object still is usable as long as it’s variable is still live and until a garbage collection cycle.

1 Like