Tween or for loop

So let’s say you have a tween, the tween goes from 1.00 to 0.00 so that’s like changing the number 100 times. But lets say you have a for = 0, 1, .05 loop which changes the transparency by .05 out of 1.00. Is it better to use the for loop or tween for better performance? Any help is appreciated!


Well Tween and Loops are a bit different. Tween lets you specify a time and it will tween something smoothly from A to B for the time specified. Loops are the opposite. They let you specify the steps, and the time taken is determined how you insert a waiting period between steps.

Overall my experience with this sort of thing, I have always found using tween to be better than loops, because loops could give a jittery visual when doing things like moving a part, while using tweens (specifically TweenService) you can specific an Easing style for a smooth movement of that part, so you dont have to include your own easing within a for loop.

--Linear Motion with a while loop

local function linearMotion(completionTime, callback)
  local executedTime = 0
  local x = 0

  while x < 1 do
    executedTime += task.wait()
    if executedTime >= completionTime then
      executedTime = completionTime

    x = (executedTime / completionTime)

Its extremely primitive but I just wanted to mention that you can make smooth linear motion using loops

You could probably feed x into TweenService:GetValue before passing it through to the callback

OP; It depends on the performance impact, I’d generally say go with tweens if you’re dealing with anything client side, but tweening on the serverside is known to be jittery.

If you want tweens to be reliable on the server, its better to implement something yourself that emulates tweens.

Heres how to use the linearMotion function I posted to change your part’s transparency from 0 to 1 over five seconds:

linearMotion(5, function(n)
  part.Transparency = n

this is assuming it works obviously, its also worth nothing that its not asynchronous and will hold up your code unlike Tweens, but I can definitely refactor that since it’s functional based

1 Like

The tween will play with more fluidity/smoothness than a for/while/repeat loop.

I don’t really care about if its jittery or not. I want to know what runs better and creates less lag.

In my experience, I found that Tweens run better and create less lag than loops. Tweens also more accurately finish at the required time.

1 Like