So I’m wondering on how to properly create a timer using RunService.Heartbeat, which involves getting the DeltaTime, and subtracting the time with it, but from my attempts, I have never been able to set it up properly, but here is the current code I was testing with:
dur = 60 -- the duration
t = dur -- the current time
repeat
local dt = RunService.Heartbeat:Wait() -- DeltaTime
t = math.max(0, t - (dt/dur)) -- Sets a Limit to the number while setting the DeltaTime divided by the duration.
print(t/dur) -- test print
until t == 0 -- will stop when this condition is met
Would this work? or am I doing something wrong?
(This is a bit vague ik. I dont have the time to complete it, which ill do later)
Instead of using repeat and until, you can use a while loop with the condition t > 0. This makes the code more readable and easier to understand.
The calculation for updating the timer should be t = t - dt, not t = math.max(0, t - (dt/dur)). This is because dt is already the time elapsed since the last frame, so you can subtract it directly from t.