How to make timer by using tick() or os.clock()

Hi developers!

Can someone explain how to make timer by using tick() or os.clock()?

I was trying to make timer by using os.clock() but here is what prints in output.
image

here is script:

local BoostStart = os.clock() 
local BoostLength = 120

local BoostEnd = BoostStart + BoostLength

for i = 1, BoostLength do
	print(BoostEnd - i)
	wait(1)
end
1 Like
local Start = tick()
local Len = 120

for i=1,Len do
print(Start-tick) -- Would be == i
wait(1)
end

I think thats what you want? You didnt explained well.

What exactly do you need this for? Is it some kind of stamina-based sprinting system? Is it a round timer in a match-based game? Something else?

I want to make boost timer (or round countdown timer)

os.clock should only be used for benchmarking, it refers to the CPU clock cycles.

You’re code example shouldn’t use this but if you want a wall-clock timestamp use os.time().

Here’s what I think you want, but there are still better alternatives depending on what you are trying to do.

local BoostLength = 120

for i = 1, BoostLength do
    local remaining_time = BoostLength - i
	print(remaining_time)
	task.wait(1) -- task.wait is usually more accurate than wait
end
2 Likes

Like what @gertkeno said, you don’t really need os.clock() or os.time() for this, you are already yielding every one second.