Number damping/ smooth increase from 0 to 1

Im attempting to smoothly increase a number value.

EG - When I input for example 1 into a textbox, i would like another textbox to go from 0 to 1 smoothly/gradually.

Any equation/tips would help me out.

2 Likes

you can use a for loop that uses steps of 0.1 or even 0.05:

local runservice = game:GetService("RunService")
for i = 0, 1, 0.05 do
   TextBox.Text = i
   runservice.RenderStepped:Wait() 
end

in combination with the Changed event on the other textbox you can get this activated and let the one that smoothly gets to the number react to the number you put into the first textbox,

local runservice = game:GetService("RunService")

function NumberOnly(text)   
   return text:gsub("%D", "")
end

Textbox1:GetPropertyChangedSignal("Text"):Connect(function()
   Textbox1.Text = NumberOnly(Textbox1.Text)
end)

Textbox1.FocusLost:Connect(function()
   if Textbox1.Text == "" then return end
   for i = 0, tonumber(Textobx1.Text), 0.05 do
      TextBox2.Text = i
      runservice.RenderStepped:Wait() 
   end
end)

this will let the Textbox2 react to Textbox1 as a loop. if put in textbox 1 a number like 6 it’ll go from 0 to 6 with steps of 0.05.
The number only function prevents the user from putting in anything else but numbers

2 Likes

just do

local number=1
local time=5
local val=0
for --[[render step]] do
if val<number then
    val = val+number / (60*time)
else 
break
end
end

I use a NumberValue and Tween it’s Value using TweenService, then on .Changed I display the new Value.
Here’s an example if the script was inside the TextBox:

local ts = game:GetService("TweenService")
local ti = TweenInfo.new(2) --How long should it take

local NV = Instance.new("NumberValue")
NV.Parent = script

NV.Changed:Connect(function(new_value)
    script.Parent.Text = tostring(new_value)
end)

local tween = ts:Create(NV, ti, {Value = 1})
tween:Play()
3 Likes

You could alternatively make your own simple function for interpolating a number–

local function LerpNumber(p0, p1, a)
	a = math.min(a, 1)
	return p0 + ((p1 - p0) * a)
end

Interpolation thus becomes as simple as:

local number = 20
local number_start = 20
local number_end = 35
local alpha = 0
local duration = 1
while alpha < 1 do
	local dt = RunService.RenderStepped:Wait()
	alpha = alpha + (dt / duration)
	number = LerpNumber(number_start, number_end, alpha)
end

You could also constantly interpolate the number variable from its current value to a target that can be updated:

local speed = 20
local number = 20
local current_goal = 35

RunService.RenderStepped:Connect(function(dt)
	number = LerpNumber(number, current_goal, dt * speed)
end)
4 Likes

So, I actually asked this question before and I got this answer:
wait(2.2)

local goal = 10 -- the number that you want it to go up to

local duration = 3 -- the duration of the increase

local start = tick()

local stop = start + duration

local t = start

local n = 0

while t < stop do
   n = math.floor(math.sin((t - start) * math.pi / 2 / duration) * goal)
   wait()
   t = tick()
end

this is a late response but you can literally lerp it using cframe just take the position x y z whatever yeah