And so on - you get the point. I know from friends telling me all the time that there is a better way to so it and to make it smooth - so a tween between a set amount of numbers, over a set amount of time.
I’m wondering if tweening values is an easy thing to do, and how would I do it.
I remember literally writing out each of the values when I first started programming! Tweening is definitely a great way to do it, you can even Lerp if you want to get fancy. Here’s a really simple solution to a mostly clean loop that really helped me learn when I first got started!
for i = 1, 10 do
script.Parent.Value = i/10 --This iterates between 0 and 1, do print(i) to see it in action!
wait(0.1)
end
If you want to get a little more advanced, you could set up some variables for it too!
local duration = 1
local amount = 10
for i = 1, amount do
script.Parent.Value = i/amount
wait(duration/amount)
end
Again, not the best way to do this or the smoothest either, but this stuff is really fundamental when you are first starting out so its worth a try!
@Barothoth I agree with your post, just adding some extention to it.
Lerp is an abbrevation of linear interpolation. You can read more about it here on Wikipedia. (Linear interpolation, 1. november 2020)
Linear interpolation in a nutshell is a function. It is used to construct a value between two other values. In programming, we know so called alpha value, which is usually recognized as parameter “t”. It is an interval unit ranged between 0 and 1.
So t = 0 → starting point; t = 1 → ending point.
This is the formula:
(a * (1 - t)) + (b * t)
Luckly, Roblox already has a built in :Lerp method.
How would you use “lerp”?
for i = 0, 1, 0.01 do
wait()
value = value:Lerp(goal, interval)
end
There are some useful tutorials available on YouTube, which include explanations on smooth interpolation of CFrames and numbers as well.
You can also use TweenService. Tweening a number is not possible from the script only, because a created “tween” interpolates values that are properties of “tweened” object.
Even more tutorials here. Tweening is almost essential now and is much more advanced then linear interpolation function.
Example code:
Like @Semistare said, you can only tween values stored outside script (including for example CFrames, which hold their properties, so they can be tweened).