So I’m working on a speedrun timer for my game, and I’ve found 2 ways to do it. I could either set a variable to 0 and every frame add the delta to that and format the time, or I could use tick() and just set it as a variable (in this example variableNameHere) and then every frame do tick()-variableNameHere and format that.
The thing is, I’m not sure which is better. Any help would be appreciated. Also, if there’s a better way please let me know. Example:
-- I wrote the code directly here so there might be some errors, but you should get the point
local rs = game:GetService("RunService")
-- Function to format the time
local function formatTime(timeVal)
local hours = math.floor(math.fmod(timeVal, 86400)/3600)
local minutes = math.floor(math.fmod(timeVal,3600)/60)
local seconds = math.fmod(timeVal,60)
return string.format("%d:%02d:%05.2f", hours, minutes, seconds)
end
-- Using delta
local totalTime = 0
local function timer(delta)
totalTime += delta
timerGui.Text = formatTime(totalTime)
end
rs:BindToRenderStep("TimerThingie", Enum.RenderPriority.First.Value, timer)
-- Using tick()
local startTime = tick()
local function timer()
timeGui.Text = formatTime(tick()-startTime)
end
rs:BindToRenderStep("TimerThingie", Enum.RenderPriority.First.Value, timer)