✓ Accepted answer
I'm not sure if Heartbeat is more effective or not than the while loop, but this is how I would do it:
local runService = game:GetService('RunService')
local maxTime = 1
local currentTime = maxTime
local function onFinished ()
print('TIMER FINISHED')
end
runService.Heartbeat:Connect(function(dt)
currentTime -= dt
if currentTime < 0 then
onFinished()
currentTime = maxTime
end
end)
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.
local function startTimer (timeLength)
local startValue = tick()
while (startValue + timeLength) > tick() do
wait(0.00001)
end
return
end
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.
1 comment
bruh, the past few questions that i asked from as early as 2 months ago got no answers. Do people just give up answering these days