Can y'all help with changing my timer to minutes?

  1. What do you want to achieve? Keep it simple and clear!
    I want to convert my timer from seconds to minutes in the script below.
  2. What is the issue? Include screenshots / videos if possible!
    When I try to convert my seconds to minutes nothing works and I’ve tried for the past week. I hope somebody can give me guidance!
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I’ve looked all over youtube and I even asked the forum here but nothing seems to work and I’m somewhat clueless about where to put everything because I’m fairly new to scripting! Also let me know if I need to create a folder in the developer hub because I can since I have my timed remoteevent named “BestTime” and I have my checkpoint sound that plays when you step on one of my checkpoints! Script 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("%.3f", currentTime) .. "\nPersonal Best: " .. string.format("%.3f", personalBest.Value)
personalBest.Changed:Connect(function()
	textLabel.Text = "Timer: " .. string.format("%.3f", currentTime) .. "\nPersonal Best: " .. string.format("%.3f", personalBest.Value)
end)

function onStartTouched(hit)
	timerStopped = false
	startTime = tick()
	currentTime = 0
	Runservice.RenderStepped:Connect(function()
		if not timerStopped then
			currentTime = tick() - startTime
			textLabel.Text = "Timer: " .. string.format("%.3f", currentTime) .. "\nPersonal Best: " .. string.format("%.3f", personalBest.Value)
		end
	end)
end

function onEndTouched(hit)
	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("%.3f", currentTime) .. "\nPersonal Best: " .. string.format("%.3f", personalBest.Value)
	end
end

workspace:WaitForChild("StartTimer").Touched:Connect(onStartTouched)
workspace:WaitForChild("EndTimer").Touched:Connect(onEndTouched)
6 Likes

To convert seconds to minutes, you can use this:

local seconds = 1234
local minutes = math.floor(seconds / 60)
-- 20

To convert to minutes and seconds, you can use this:

local seconds = 1234
local minutes = math.floor(seconds / 60)
-- 20
local seconds = seconds % 60
-- 34
5 Likes

Yo I need a little guidance on where to put that, what line etc. Do I put it above or below here somewhere?

textLabel.Text = "Timer: " .. string.format("%.3f", currentTime) .. "\nPersonal Best: " .. string.format("%.3f", personalBest.Value)
personalBest.Changed:Connect(function()
	textLabel.Text = "Timer: " .. string.format("%.3f", currentTime) .. "\nPersonal Best: " .. string.format("%.3f", personalBest.Value)
end)
2 Likes

Anywhere that displays the seconds, (for example) instead of using:

tostring(seconds)

You can use:

tostring(math.floor(seconds / 60))..":"..tostring(seconds % 60)

Or a more specific example:

textLabel.Text = "Timer: " .. tostring(math.floor(seconds / 60)) .. ":" .. tostring(seconds % 60)
.. "\nPersonal Best: " .. tostring(math.floor(bestSeconds / 60))..":"..tostring(bestSeconds % 60)
1 Like

Oh wait! Do you recommend that I replace those 2 lines that I sent with the code you sent or do I just fill it in? I’m just confused on where to put everything because I’ve never done anything like this in my life. I changed it to this below I don’t know if its correct or not:

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: " .. tostring(math.floor(seconds / 60)) .. ":" .. tostring(seconds % 60)
	.. "\nPersonal Best: " .. tostring(math.floor(bestSeconds / 60))..":"..tostring(bestSeconds % 60)

function onStartTouched(hit)
	timerStopped = false
	startTime = tick()
	currentTime = 0
	Runservice.RenderStepped:Connect(function()
		if not timerStopped then
			currentTime = tick() - startTime
			textLabel.Text = "Timer: " .. tostring(math.floor(seconds / 60)) .. ":" .. tostring(seconds % 60)
				.. "\nPersonal Best: " .. tostring(math.floor(bestSeconds / 60))..":"..tostring(bestSeconds % 60)
		end
	end)
end

function onEndTouched(hit)
	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: " .. tostring(math.floor(seconds / 60)) .. ":" .. tostring(seconds % 60)
			.. "\nPersonal Best: " .. tostring(math.floor(bestSeconds / 60))..":"..tostring(bestSeconds % 60)
	end
end

workspace:WaitForChild("StartTimer").Touched:Connect(onStartTouched)
workspace:WaitForChild("EndTimer").Touched:Connect(onEndTouched)
1 Like

I think replacing those two lines should work, but you’ll have to test it yourself to see

2 Likes

Oh Lord it only shows “Label” now and not the times! You mind looking at the updated script I sent above your last comment and checking what mistakes I made? It’s saying “unknown global ‘seconds’

1 Like

change seconds to currentTime and bestSeconds to personalBest.Value

2 Likes

I change them on every line? So all those lines should look like this

textLabel.Text = “Timer: " … tostring(math.floor(currentTime / 60)) … “:” … tostring(currentTime % 60)
… “\nPersonal Best: " … tostring(math.floor(personalBest.Value / 60))…”:”…tostring(personalBest.Value % 60)

2 Likes

Try this

local currentTime = 0
local timerStopped = false
local Runservice = game:GetService("RunService")
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local personalBest = script:WaitForChild("PersonalBest") or math.huge
local replicatedStorage = game:GetService("ReplicatedStorage")

local textLabel = script.Parent

local function round(number) -- round to 3 decimal places
	number *= 1000
	number = math.floor(number)
	number = number / 1000
	return number
end

local function Format(Int)
	return string.format("%02i", Int)
end

local function convertToMS(Seconds)
	Seconds = round(Seconds)
	local Minutes = (Seconds - Seconds%60)/60
	Seconds = Seconds - Minutes*60
	return Format(Minutes)..":"..Seconds
end

local function UpdateText()
	textLabel.Text = "Timer: " .. convertToMS(currentTime) .. "\nPersonal Best: " .. convertToMS(personalBest.Value)
end
	
personalBest:GetPropertyChangedSignal("Value"):Connect(UpdateText)

function onStartTouched(hit)
	timerStopped = false
	local startTime = tick()
	currentTime = 0
	Runservice.RenderStepped:Connect(function()
		if not timerStopped then
			currentTime = tick() - startTime
			UpdateText()
		end
	end)
end

function onEndTouched(hit)
	if not timerStopped then
		timerStopped = true
		if currentTime > 0 and currentTime < personalBest.Value then
			personalBest.Value = currentTime
			replicatedStorage.BestTime:FireServer(currentTime)
		end
		UpdateText()
	end
end

workspace:WaitForChild("StartTimer").Touched:Connect(onStartTouched)
workspace:WaitForChild("EndTimer").Touched:Connect(onEndTouched)
1 Like

Yo I think it might be me doing something wrong because I copied and pasted your script but now my gui only displays “label” as the timer in test mode! Any idea of what I should do? Like rename my gui or something?

Can you send what the gui looks like with everything expanded and the script inside? It’s definitely an issue with parenting.

2 Likes

Yeah one second ima screenshot the replicatedstorage area too

1 Like

Here


yes, did it work? sorry for late reply

1 Like

can you at least try explain this?

1 Like

Here are the steps to ensure the script is correctly linked to your GUI:

  1. Make sure the script is a child of the GUI element in the hierarchy.
  2. The script should be placed in the same location as the GUI element you want to control. For example, if your GUI is in the StarterPack, the script should also be in the StarterPack.
  3. Ensure that the GUI element has a TextLabel or a similar object to display the timer text.
  4. Double-check the spelling and capitalization of the GUI element’s name in the script. It should match the actual name of the GUI element in the hierarchy.
1 Like

Is the ScreenGui in StarterGui and have you made sure you checked the exact TextLabel? It works fine for me, perfectly.
For the reply above, all the points recommended by him or should I say
ChatGPT
have already been done.

Edit it has already been solved please send a post that solved the problem then mark it as a solution so people don’t get confused

1 Like

here’s my solution in his other post

1 Like