Timer for my obby

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

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.

Roblox is down rn so i cant be sure it works

3 Likes

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?

4 Likes

You put the function in between here

also i made the mistake of deleting return and also i forgot your currentTime is in string format so it should look like this

local function toMS(s)
	local MS = string.format("%02i:%02i", s/60%60, s%60)
    return MS
end

and im pretty sure the local variables I put inside of the functions wont work outside lol so you gotta make a separate variable outside

local msFormat = toMS(currentTime)

put this between these two lines and replace “currentTime” with msFormat from here on out

2 Likes

Preciate you gang and have a wonderful rest of your day :sunglasses: :facepunch:t4:

3 Likes

Wait how do I make a separate variable outside?

2 Likes

Sorry for the late reply just put it under the rest of your variables at tthe top

1 Like

don’t forget to mark it as the solution

1 Like

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

1 Like

I got you man I won’t forget it. Its just I need a little more help.:sunglasses:

1 Like

you can just copy and paste it

1 Like

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)

1 Like

if you still have questions or if its not working please ask

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.