How to tween values

Hi there, so I am not much of a scripter at all, but I have come to this thread to ask how to tween values.

So pretty much at the moment I’m literally doing something along the lines of:

script.Parent.Value = 0
wait (.5)
script.Parent.Value = 1
wait (.5)

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.

Hope this makes sense,
Thanks!

2 Likes

Hope this helps!

1 Like

I recommend looking at these two articles.

3 Likes

You can use TweenService for this.

I don’t actually remember if you can use TweenService on values, only way to find out is by trying.

local TweenService = game:GetService("TweenService")

local TweenInformation = TweenInfo.new(0.5, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, false, 0)
local Tween = TweenService:Create(script.Parent, TweenInformation, {Value = 1}):Play()
3 Likes

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!

2 Likes

@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).

5 Likes

how would i do this with a leaderstat value? with mine the leaderstat value just goes back to 0

apologies wrong area lool :slight_smile: forgive that