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 :
(The one that i selected)
And the text label that i want to animate is here :
(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 ?
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.
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().
Still not precise if i put 500 sometimes it give me 499 or 501
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.
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
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.