What is the right way to use TweenService?

Hi, a few days ago I wanted to animate a model with Tweenservice and I came across this tutorial with this code that apparently works well

while true do
	tweenservice:Create(primary, TweenInfo.new(1,Enum.EasingStyle.Sine,Enum.EasingDirection.InOut),{CFrame = primary.CFrame * CFrame.new(primary.Size.X + .1,0,0)}):Play()
	wait(1)
end

My question is whether constantly creating Tweens reduces the overall performance of the game and whether this is good practice.
Another question is that a Tween is an Instance. So it shouldn’t be destroyed in every iteration? In general I have never seen anyone destroy a Tween (i.e. tween:Destroy())

1 Like

Not at all, you should create one Tween and use it multiple times. Unfortunately in this case, a new Tween has to be created since the goal changes.

Yes, Tweens are an Instance. Destroy them if you can however they’ll only use a miniscule amount of memory if you don’t.


For your given code snippet, I’d like to suggest a change. Tweens have a Completed event (RBXScriptSignal) which obviously has a :Wait() method, you should try to use this instead of a imprecise wait().

ie.

local tween = tweenservice:Create(primary, TweenInfo.new(1,Enum.EasingStyle.Sine,Enum.EasingDirection.InOut),{CFrame = primary.CFrame * CFrame.new(primary.Size.X + .1,0,0)});
tween:Play();
tween.Completed:Wait();
5 Likes

in case the animation was to be played all the time. Even so, the use of memory would be negligible?

Pretty much, I’ve had no real issues with memory when not Destroying Tweens. I’d definitely recommend doing it however.

1 Like

This is really valuable. Thanks for helping me.

1 Like