Answered by
3 years ago Edited 3 years ago
I'm not sure if Heartbeat is more effective or not than the while loop, but this is how I would do it:
01 | local runService = game:GetService( 'RunService' ) |
04 | local currentTime = maxTime |
06 | local function onFinished () |
07 | print ( 'TIMER FINISHED' ) |
10 | runService.Heartbeat:Connect( function (dt) |
13 | if currentTime < 0 then |
Heartbeat is only for the client, but for the server you would need something else.
Instead, we can use tick(). If we look up Tick() it returns the number of seconds (including milliseconds) from the Unix Epoch. So we can make a more accurate timer for the server using the following code.
01 | local function startTimer (timeLength) |
02 | local startValue = tick() |
04 | while (startValue + timeLength) > tick() do |
The code above should be very accurate, and if you want it to be more accurate just decrease the wait() even more. But it would be very costly to do so. I am unsure of any better ways to make a timer, so correct me if I'm wrong.
EDIT: SORRY! My mistake, both would work on the server. Thanks to OfficerBrah for correcting me.