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
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
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 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)
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