TweenService:Create() Cleanup

Does TweenService:Create() clean up after you :Play()? I’m not sure if its a big issue but if I am creating a ton of animations, I don’t want a memory leak…

3 Likes

I’m pretty sure it’s like any other variable, leaks if you keep a reference but garbage collects if not. You can always call destroy on it if you’re worried about any memory leaks.

2 Likes

Whenever I use TweenService, instead of using it directly I make a function like this one below.

local tService = game:GetService("TweenService")

function twn(obj,info,prop)
    local t = tService:Create(obj,info,prop)
    t:Play()
    c = t.Completed:Connect(function()
        t:Destroy()
        c:Disconnect()
    end
end
3 Likes

This statement:

TweenService:Create(...):Play()

Does not leak memory. The Create call returns an object, but you don’t assign it to anything (it’s anonymous), so it will be a candidate for garbage collection right after this statement, since nothing else refers to it.

If you were to do this:

local tween = TweenService:Create(...)
(...)
tween:Play()

Then tween will be cleaned up when it goes out of scope (i.e. end of the control block: function/if/for/while statement, etc), provided you hadn’t assigned to anything else in between.

37 Likes