How to make a number goes up / down smoothly?

  1. What do you want to achieve?
    Hello ! I want to make a number goes up or down smoothly. In my game there is a value that change a lot and i want to do something. Instead if i put in the value 500 and it display 500 on the surface gui it goes for example from 0 to 500 one by one.

  2. What is the issue?
    I don’t know how to do it

  3. What solutions have you tried so far?
    I searched and it didn’t worked.

3 Likes
local txtLabel = script.Parent

local timeTo500 = 5


local isRunning = false
local function countNumEffect(startN, endN, inc, waitTime)
isRunning = true

for i = startN, endN, inc do

	if not isRunning then return end

	txtLabel.Text = i
	wait(waitTime)
	end
	
	txtLabel.Text = endN
	
	isRunning = false
end



local startNum = tonumber(txtLabel.Text)
local endNum = 0

local negator = 0
local increment = 1

if endNum < startNum then negator = +1; increment = +1 end


local waitTime = (negator + ((timeTo500 / 500) * math.abs(endNum - startNum))) / 500


if waitTime < (1/30) then
	local numsPerWait = math.ceil((1/30) / waitTime)
	increment = increment * numsPerWait
end


isRunning = false
countNumEffect(startNum, endNum, increment, waitTime)

Edit: Sorry for it being cut to thirds, I did not use the code casting well.

2 Likes

My value change a lot so how to i animate the number with the value?

1 Like

What do you mean by change a lot? do you want to make it slower?

2 Likes

No when my value example is at 800 or all other numbers will it be animated ?

1 Like

oh an there is an error at line 31

1 Like

You can maybe use Lerping.

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

local SPEED = (25);
local numberGoal = (0); -- // Change it when needed to update the display
function TextUpdate(dt)
    script.Parent.Text = tostring(Lerp(tonumber(script.Parent.Text), numberGoal, dt * SPEED)
end;

runService.RenderStepped:Connect(TextUpdate)
1 Like

Isn’t that a bit overcomplicated? Could just do

while value ~= endValue do
	wait(waitTime)
	value += increasement
end
2 Likes

Thank you for your reply There is an error at line 10

1 Like

What is the error there? Send the error itself

1 Like

the error is in red in the script

1 Like

Very simple and good, I thought about that first but I wanted to upgrade it so It will be with speed and Starting Ending.

2 Likes

What does the output print? A red line doesn’t really specify me the problem.

1 Like

Wait it does this
enderStepped event can only be used from local scripts

1 Like

What’s the error?
Specify, please.

1 Like

Try this:

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

local SPEED = (15);
local numberGoal = (0); -- // Change it when needed to update the display
function TextUpdate(dt)
	script.Parent.Text = math.ceil(tostring(Lerp(tonumber(script.Parent.Text), numberGoal, dt * SPEED)))
end;

runService.Heartbeat:Connect(TextUpdate)

Put it inside the TextLabel.

1 Like

in the script that you gave me ?

1 Like
function TextUpdate(dt)
	script.Parent.Text = tostring(Lerp(tonumber(script.Parent.Text), numberGoal, dt * SPEED))
end;
1 Like

Positive.
The script worked for me great.

1 Like

1 Like