Hey does anyone know how to convert seconds to minutes on a timer? I’m fairly new to scripting and I’m a little lost. My timer works just fine but instead of saying “65.00” I would rather it read “1.05.00” when 65 seconds have passed. This is the local script I have down below!
local currentTime = 0
local timerStopped = false
local Runservice = game:GetService("RunService")
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local personalBest = player:WaitForChild("PersonalBest") or math.huge
local replicatedStorage = game:GetService("ReplicatedStorage")
local textLabel = script.Parent
textLabel.Text = "Timer: " .. string.format("%.2f", currentTime) .. "\nPersonal Best: " .. string.format("%.2f", personalBest.Value)
personalBest.Changed:Connect(function()
textLabel.Text = "Timer: " .. string.format("%.2f", currentTime) .. "\nPersonal Best: " .. string.format("%.2f", personalBest.Value)
end)
function onStartTouched(hit)
if hit.Parent == player.Character then
timerStopped = false
startTime = tick()
currentTime = 0
Runservice.RenderStepped:Connect(function()
if not timerStopped then
currentTime = tick() - startTime
textLabel.Text = "Timer: " .. string.format("%.2f", currentTime) .. "\nPersonal Best: " .. string.format("%.2f", personalBest.Value)
end
end)
end
end
function onEndTouched(hit)
if hit.Parent == player.Character then
if not timerStopped then
timerStopped = true
if currentTime > 0 and currentTime < personalBest.Value then
personalBest.Value = currentTime
replicatedStorage.BestTime:FireServer(currentTime)
end
textLabel.Text = "Timer: " .. string.format("%.2f", currentTime) .. "\nPersonal Best: " .. string.format("%.2f", personalBest.Value)
end
end
end
workspace:WaitForChild("StartTimer").Touched:Connect(onStartTouched)
workspace:WaitForChild("EndTimer").Touched:Connect(onEndTouched)
Im also a beginner scripter so i apologize in advance for the mistakes I will make. I found a code from this post that already has the logistics.
local function toMS(s)
local MS = string.format("%02i:%02i", s/60%60, s%60)
local msFormat = tonumber(MS)
end
It creates a function that conveniently converts the value of the variable inside of the parameters to minute and second form.
In case you dont know what to do next you simply add in currentTime inside of the parameters of toMS() so that it sets the new variable msFormat to minute and second format. Now you should just replace currentTime with msFormat.
Preciate it bacon brother but where do I put that little section of code? Do I add another local script inside of my timer gui or do I add it in somewhere?
Ohh so I put it under the first 7 lines of code? Do I just copy your code you sent and paste it somewhere or do I have to create my own? I don’t really know how to make my own, I tried
I realized that i’m not being clear enough so ill explain more
put this under the variables because this basically creates a function that will convert a string into minute to second form
it doesnt do anything to the actual code it just creates a function that can be used later
in this line of code we call the function by writing toMS() and add currentTime inside of the parenthesis toMS(currentTime) so that it converts currentTime to minute to second form. local msFormat = toMS(currentTime)
is going to be set to whatever toMS(currentTime) returns which will be the minute to second form, which is what we want so you can just replace all of the places you used currentTime after creating the variable. (we still need currentTime before we created msFormat because it does the actual counting)