What do you want to achieve?
I wanted a timer counting up with 3 rational digits like 0.001 and going up so.
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
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
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
.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.
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)
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