I’m trying to make a Speedrun Timer, and I know I should tick()
instead of wait()
or task.wait()
. But, how could I do that?
Here’s the full script:
RemoteClient.OnClientEvent:Connect(function(info)
if info == "StartTimer" then
local Seconds
local Minutes
local Hours
local DataStoreTime
local s = 0
local m = 0
local h = 0
while true do
task.wait(0.001)
s = s + 1 -- Seconds
if s <= 9 then
Seconds = "0" .. s
else
Seconds = tostring(s)
end
if m <= 9 then -- Minutes
Minutes = "0" .. m
else
Minutes = tostring(m)
end
if s == 99 then
s = 0
m = m + 1
end
if h <= 9 then -- Hours
Hours = "0" .. h
else
Hours = tostring(h)
end
if m == 60 then
m = 0
h = h + 1
end
SpeedrunTimer.Text = Hours .. ":" .. Minutes .. ":" .. Seconds
end
end
end)
local lasttask=0 --variable to keep track of the last incident
local numberinsec=3 --to compare time
if tick()-lasttask<numberinsec then
return
end
lasttask=tick()
How about you use a high resolution timer like os.clock
and on the client you just put in a RunService RenderStepped loop a function that displays the timer?
Its probably a good idea to just use t += task.wait()
, task.wait
returns the time that the function paused execution for, and it waits better than the deprecated wait
function.
local t = 0
while true do
t += task.wait()
local s = math.floor(t) -- <- use this to render the text
end
1 Like
You have no need for any of that:
local FORMAT = "%d:%02d:%02d"
local HOURS = 3600
local MINUTES = 60
local Timer = script.Parent
local secondsElapsed = 0
while true do
local hours = secondsElapsed // HOURS
local minutes = secondsElapsed % HOURS // MINUTES
local seconds = secondsElapsed % MINUTES
Timer.Text = string.format(FORMAT, hours, minutes, seconds)
task.wait(1)
secondsElapsed += 1
end
1 Like
How could I be able to do that?
While thats a good a approach it can be improved further using the high resolution timer os.clock
local Start = os.clock()
game:GetService("RunService").PreRender:Connect(function()
-- code to show timer here
local CurrentTime = os.clock() - Start -- this is in seconds
end)