Making multiple digits count up timer

  1. What do you want to achieve?
    I wanted a timer counting up with 3 rational digits like 0.001 and going up so.

  2. What is the issue?
    I’ve tried making it by adding 0.001 every 0.001 seconds but it results that the script runs too slow and the time displayed which is counting up is not fast and accurate, and sometimes end up at 0.9999999999998

  3. What solutions have you tried so far?
    I’ve tried looking for different posts but none of them solved my question so I’m here to ask about it

My script:

While true do
  script.Parent.Text = script.Parent.Text + 0.001
wait(.001)
end

try use a repeat until script

e.g
local text = script.Parent.Text

repeat
Text = Text + 0.001
wait (0.001)
until
-condition
end

Your use is very undefficient.
Try using this one instead

function Timer(s: int)
  return ('%02i:%02i'):format(s/60%60, s%60)
end

for lengh = timercounter, 0, -1 do -- replace timercount with the number of time you want to wait for
  script.Parent.Text = Timer(lengh)
  wait(timercount)
end
1 Like

.001 is much too fast. Best you can get is .033 with wait but I still wouldn’t trust the accuracy there. I believe RenderStepped has a Delta time parameter, that might be your best bet. It also literally runs at your frame rate, so if even if you could make the counter run faster you wouldn’t see it.

1 Like

If you know how to you could link this up with what you want, a timer or countdown. It is a timemodule I made. https://devforum.roblox.com/t/time-conversion-module/1374242
You can easily link it up to be honest. Its easy to use as well.

Hope it helps!

Not sure its the best solution and there are some things you would have to work out but you could use a NumberValue and the TweenService. Then just use Stepped or RenderStepped to format and display the number for whatever your purpose is.

e.g. This is not perfect. Only here for example

local TS = game:GetService("TweenService")
local RS = game:GetService("RunService")

stringFormat = "%.3f"

numberCounter = Instance.new('NumberValue', workspace)
numberCounter.Value = 0.0

local tweenInfo = TweenInfo.new(5, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut, 0, false)

local tween = TS:Create(numberCounter, tweenInfo, {Value = 1})

tween:Play()

RS.Stepped:Connect(function(step)
print(string.format(stringFormat, numberCounter.Value))
end)

ExamplePrint

NOTE: this video used a tween time of 30s and a target value of 30. Not that values specified in the example code

The easiest way to make timers is to just keep track of the time you started:

local start = tick()

while wait() do -- or could be RunService.Stepped or whatever
	local now = tick()
	local diff = now - start
	script.Parent.Text = string.format("%.03f", diff)
end

edit: used tick() instead of os.time() so it’s more accurate

It worked correctly, thanks! :smiley:

So I just found a problem, I cannot seem to end the tick function. The timer starts correctly but it cannot end.

I didn’t add anything about ending it.

You’d just save the tick() at the time you end the timer.

I see, thanks! I figured out a way already! :slight_smile: