-
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. -
What is the issue?
I don’t know how to do it -
What solutions have you tried so far?
I searched and it didn’t worked.
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.
My value change a lot so how to i animate the number with the value?
What do you mean by change a lot? do you want to make it slower?
No when my value example is at 800 or all other numbers will it be animated ?
oh an there is an error at line 31
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)
Isn’t that a bit overcomplicated? Could just do
while value ~= endValue do
wait(waitTime)
value += increasement
end
Thank you for your reply There is an error at line 10
What is the error there? Send the error itself
the error is in red in the script
Very simple and good, I thought about that first but I wanted to upgrade it so It will be with speed and Starting Ending.
What does the output print? A red line doesn’t really specify me the problem.
Wait it does this
enderStepped event can only be used from local scripts
What’s the error?
Specify, please.
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.
in the script that you gave me ?
function TextUpdate(dt)
script.Parent.Text = tostring(Lerp(tonumber(script.Parent.Text), numberGoal, dt * SPEED))
end;
Positive.
The script worked for me great.