The problem: I want a variable “a” to increase from 0 to 1 in the span of 2 seconds (no greater, no less) locally, but I have no idea how to do this. Tweenservice will not work since this is a variable and not a value, but I’m hoping that there’s some way to do this with runservice and some math.
What do you mean variable to increase?
Let’s say I defined a as equal to 0. How would I have it smoothly increase to 1?
local a = 0
Yes, you can tween that number.
I thought you can only tween something from an instance not a variable?
Properties of an Instance contains data types, and data types can be tweened.
You can just try that or do a numerical for loop
This is a variable in a script not and int value in an instance.
you could use tween to tween the value over that time
It’s not a value, it’s a variable in a script.
this would be the easiest way of setting it up else just use a loop and add to the value say .1 each loop with a wait time of .1 something like this may work also
a = 0
increaseby = .05 -- change this if you want it even smaller increments
while a < 1 do
a = a + increaseby
print(a)
wait(increaseby)
end
This is what I used before, but the issue is I have to increase the value a bit every frame or else it looks choppy. I also need to do it for a specific amount of time which means that the variable cant always increase at a steady level.
that should do what your asking without tween you can change that value down to say .01 and that would be base increment each loop. you could also do this in a repeat
There are 2 ways to complete your request.
Create a NumberValue object and use TweenService to tween the instance’s value.
Or
Use a LerpNumber() method that is binded to RunService:BindToRenderstepped and call the method every frame using deltatime.
local function LerpNumber(a, b, t)
return a + (b - a) * t
end
I recommend using a NumberValue since it’s better to allow the service tackle the tweening for you. Instead of having to bind and do the calculations on RenderStepped. Then unbinding when its done.
If you do use the LerpNumber method. You would have to provide t yourself. I’d use BindToRenderStep since you can increase t every frame (Just in case the player’s machine lags behind within 2 seconds) using deltatime
This can do something very close to what you need but as we all have said you should probably use Tween because it is more accurate and smooth for changing values over time
a = 0
incrementby = .05
inctime = 1
num = inctime/incrementby
for i = 1, num do
a = a + incrementby/inctime
print(a)
wait(inctime/num)
end
That second method worked well, thanks.