How to make a number goes up / down smoothly?

does it work when you put script.Parent.Text to tonumber()?

For better explaining.

The value that i want to animate ONLY on the Surface gui is here :
image
(The one that i selected)

And the text label that i want to animate is here :
image
(the one called Temperature)

Use this script, and change NumberGoal’s value for it to work.

local runService = (game:GetService("RunService"));
function Lerp(a, b, m)
	return a + (b - a) * m
end;

local SPEED = (20);
local numberGoal = Instance.new("NumberValue"); -- // Change it when needed to update the display
numberGoal.Parent = script.Parent
numberGoal.Name = "NumberGoal"

local currentNumber = (script.Parent:WaitForChild("CurrentNumber"));
function TextUpdate(dt)
	currentNumber.Value = math.round(Lerp(currentNumber.Value, numberGoal.Value, dt * SPEED))
	script.Parent.Text = tostring(currentNumber.Value)
end;

runService.Heartbeat:Connect(TextUpdate)

Edit: UPDATED CODE. Tested it and worked.

wait current number needs to be my temp value ?

1 Like

CurrentNumber should not be changed, rather NumberGoal.
Try the updated code in the same comment that you responded to.

That’s an error in regard of your code. You referenced ‘CurrentNumber’ wrongly.

The script now work but there is an issue when i change the NumberGoal if i put example 200 it says 203

Are you using the updated version I added?
Re-copy paste it from that comment.

I use that is it good ?

It still remains with the math.ceil(), which rounds numbers to the highest number possible.
I updated the comment with the newer version that uses math.round().

1 Like

Still not precise if i put 500 sometimes it give me 499 or 501

Script i use :

That’s because whenever Lerping it doesn’t linearnly interpolate until the requested number, or just takes a delay to reach that number. I don’t think I got anything to do with it. Try using TweenService.

how do i use tweenservice in that

and when i change the temperature value it reduce it i don’t want the script to change it

Delete the ‘CurrentNumber’ value.
Insert a new Vector3 value, named it ‘CurrentNumber’, and leave it there.
Test this code and it should work:

local tweenService = (game:GetService("TweenService"));
local runService = (game:GetService("RunService"));

local numberGoal = Instance.new("NumberValue");
numberGoal.Parent = script.Parent
numberGoal.Name = "NumberGoal"

local currentNumber = (script.Parent:WaitForChild("CurrentNumber"));
function TextUpdate()
	tweenService:Create(currentNumber, TweenInfo.new(1), {Value = Vector3.new(numberGoal.Value, 0, 0)}):Play()
end;

numberGoal:GetPropertyChangedSignal("Value"):Connect(TextUpdate)

function Heartbeat(dt)
	script.Parent.Text = math.round(tostring(currentNumber.Value.X))
end;

runService.Heartbeat:Connect(Heartbeat)

Edit: Doesn’t necessarily have to be a Vector3 value, you can use a normal NumberValue, and then tween it’s value. I have no clue why I used Vector3.

1 Like

If you still wanted to use lerping youd just have to change the math for the alpha value a bit

Instead of doing dt * SPEED, you could do

math.clamp((dt * SPEED)/math.abs(numberGoal.Value - currentNumber.Value),0,1)

It gives that
image

Delete the existing CurrentNumber, again.
Reinsert a NumberValue and named it ‘CurrentNumber’.
And use this:

local tweenService = (game:GetService("TweenService"));
local runService = (game:GetService("RunService"));

local numberGoal = Instance.new("NumberValue");
numberGoal.Parent = script.Parent
numberGoal.Name = "NumberGoal"

local currentNumber = (script.Parent:WaitForChild("CurrentNumber"));
function TextUpdate()
	tweenService:Create(currentNumber, TweenInfo.new(1), {Value = numberGoal.Value}):Play()
end;

numberGoal:GetPropertyChangedSignal("Value"):Connect(TextUpdate)

function Heartbeat(dt)
	script.Parent.Text = math.round(tostring(currentNumber.Value))
end;

runService.Heartbeat:Connect(Heartbeat)

It worked fine for me when testing, no matter for what number.

4 Likes