Quick Question - Tweening

As a learning scripter I was messing with TweenService and had an idea. Rather than using a variable to store a created tween, like this:
local Tween = tweenservice:Create(blah blah blah)

I put it in like this:

I just wanted to know if this is efficient or if it will slow the server down over time for repeated uses as it is creating a tween each time, or if it isn’t creating a new tween every single time and I’m just fretting. Thanks!

3 Likes

It should be fine, there probably isn’t much of a negative effect on the server because of the amount of tweens. I didn’t do any research, just a guess. Happy coding!

Your code could error if TweetService:Create failed to return a Tween. I am unsure if that is possible though so I would say this would be fine to do.

2 Likes

If you mean play it on the same line as you created it, no that’s fine. That is what I always do and I never have problems with it. I typically only store the tween in a variable if I need to wait on the Completed event.

2 Likes

I would recommend destroying the tween after it finishes playing to avoid memory leaks but something that little should run fine.

Tweens are objects, so they behave like any other instance. If you are not holding a strong reference to them (like the code in the OP) or are declaring them only within a certain scope, then they will get garbage collected once they go out-of-scope. Tweens can also be overwritten by other ones.

Once upon a time ago, I didn’t have this knowledge and I actually end up posting some pretty bad stuff in relation to it. I was corrected and gradually came around to learn about it myself. See these threads for information (as well as some of my dreaded past…):

5 Likes