How can I make this into minutes and not only seconds?

Hello! I am trying to make a stopwatch script go into minutes and not only seconds.

Here is what I have rn:

local timeFormat = "%.3f" 
local tostr = tostring
local format = string.format

game.ReplicatedStorage:WaitForChild("TimeTrials").Changed:Connect(function()
	script.Parent.Label.Text = format(timeFormat, tostr(game.ReplicatedStorage:WaitForChild("TimeTrials").Value))
end)

Output for 1 Minute, 3 seconds, and 3427093 miliseconds:
63.342

What i’m trying to do is this:
1:03.342


If you can help, please let me know. Thanks, WE

you can do some math to convert seconds to minutes, dk the formula check on google

1 Like

You can use the modular(%) function, as it gives a remainder.

print(63.342 % 60) -- 3.342

So you can get the time by doing:

local Number = 63.342 % 60
print(tostring((63.342 - Number) / 60) .. ":" .. Number) -- 1:3.342
1 Like

So assuming game.ReplicatedStorage:WaitForChild("TimeTrials") is some sort of numberValue, then you can use the format specifier "%d:%.3f"

local ReplicatedStorage = game:GetService("ReplicatedStorage") -- for ez access
local timeTrials = ReplicatedStorage:WaitForChild("TimeTrials")
local label = script.Parent:WaitForChild("Label")

local TIME_FORMAT = "%d:%.3f"

timeTrials.Changed:Connect(function(new: number) -- ValueBases have a non-inherited Changed event which passes new value
	label.Text = TIME_FORMAT:format(math.floor(new/60), new%60)
end)

since you want to keep the decimal up to the third place, you don’t mod or anything, also obviously dividing by 60 and rounding it down to get the minutes

2 Likes

Yea that works, but the only thing is if seconds is < 9 then, it would show as 0:8.845 and not 0:08.845. Otherwise, that works.

and
image

My bad, you do mod by 60, since we are talking about time, I can’t think of a one-liner pattern for this, so I recommend for now just getting the remainder then concatenating it back in :\

So this would make your time format instead be "%d:%02d", so that it pads a zero at the beginning of the seconds, if it is <2 digits

local remainder = new%1
label.Text = TIME_FORMAT:format(math.floor(new/60), new%60)
label.Text ..= string.format("%.3f", remainder)
1 Like

Were super close. The only problem is this:
image
1 second, 706 miliseconds

Code:

local TIME_FORMAT = "%d:%02d"

timeTrials.Changed:Connect(function(new: number) -- ValueBases have a non-inherited Changed event which passes new value
	local remainder = new%1
	label.Text = TIME_FORMAT:format(math.floor(new/60), new%60)
	label.Text ..= string.format("%.3f", remainder)
end)

Also it works fine if we remove the miliseconds.
image

"%d:%.02d"

Missed a period.

Also you can combine the formats :slight_smile:

local secs = 63.342

local function formatTime(seconds)
    return string.format("%d:%.02d.%.03d", seconds/60, seconds%60, seconds*1000%1000)
end

print(formatTime(secs)) -- 1:03.342
1 Like