Seconds To Minutes Conversion

So this script works just fine, but I want to know how exactly would I do a sort of seconds to minutes conversion? I am new to scripting and the current topics only give a script but no exact description as to how they did it. If someone could help me out that would be amazing, thanks!

local RoundLength = 5
local IntermissionLength = 50
local InRound = game.ReplicatedStorage:FindFirstChild("Remotes").InRound
local Status = game.ReplicatedStorage:FindFirstChild("Remotes").Status

local function roundTimer()
	while wait() do
		for i = IntermissionLength , 1 , -1 do
			InRound.Value = false
			wait (1)
			Status.Value = i
		end
		for i = RoundLength , 1 , -1 do
			InRound.Value = true
			wait (1)
			Status.Value = i
		end
	end
end

^ That is the part of my script that deals with the countdown if it’s needed

There is 60 seconds in a minute. Therefore the fraction of a current minute you’re at is the seconds / 60. You would want to floor this to get the current whole minute.

math.floor(seconds / 60)

1 Like

Yeah I was under the impression I would have to use math, but what about the part where I would need the colons for example separating the minutes and seconds?

local Time = 120
local min, sec = tostring(math.floor(Time/60)), tostring(Time%60)
if #sec == 1 then
   sec = sec.."0"
end
local FixedTime = tostring(min)..":"..tostring(sec)

print(FixedTime) -- prints 2:00

Fairly simple code to separate the two of them using a colon.

7 Likes

This The old example actually prints 2:0.

You’d want to format your output. Such as with string.format.

string.format('%f:%02f', min, sec)

Fixed that in the most recent edit, I typically avoid using string.format as I am not particularly skilled with it, nor have I taken the time to learn the actual string codes.

I appreciate the suggestion though truly, thank you.

I have also seen similar code using %f though I’ve also seen code use %d, I’m assuming %f refers to a string value and %d refers to a number value?

Lua handles both %d and %f (and maybe %g too) the same way. Not sure if there are any functional differences.

1 Like

Here is a Time ModuleScript I made that might help you out

local TimeModule = {}

--> PRIVATE 

local function FormatTime(int)
	return string.format("%02i",int)
end

--> PUBLIC
function TimeModule:GetSeconds(Type,Time)
	if Time == nil then error("Conversion Requires Second Parameter.") return end
	if(Type == "Minutes")then
		return(Time * 60)
	elseif(Type == "Hours")then
		return(Time * 60 * 60)
	else
		error("Attempt to convert "..tostring(Type).." to Seconds. Must be Minutes or Hours.")
	end
end

function TimeModule:GetMinutes(Type,Time)
	if Time == nil then error("Conversion Requires Second Parameter.") return end
	if(Type == "Seconds")then
		return(Time / 60)
	elseif(Type == "Hours")then
		return(Time * 60)
	else
		error("Attempt to convert "..tostring(Type).." to Minutes. Must be Seconds or Hours.")
	end
end

function TimeModule:GetHours(Type,Time)
	if Time == nil then error("Conversion Requires Second Parameter.") return end
	if(Type == "Seconds")then
		return(Time / 60 / 60)
	elseif(Type == "Minutes")then
		return(Time / 60)
	else
		error("Attempt to convert "..tostring(Type).." to Hours. Must be Seconds or Minutes.")
	end
end

function TimeModule:ConvertToTime(Seconds,Precision)
	local Minutes = (Seconds - Seconds%60)/60
	Seconds = Seconds - Minutes*60
	local Hours = (Minutes - Minutes%60)/60
	Minutes = Minutes - Hours*60
	if(Precision == "HMS")then
		return FormatTime(Hours)..":"..FormatTime(Minutes)..":"..FormatTime(Seconds)
	elseif(Precision == "HM")then
		return FormatTime(Hours)..":"..FormatTime(Minutes)
	elseif(Precision == "H")then
		return FormatTime(Hours)
	elseif(Precision == "MS")then
		return FormatTime(Minutes)..":"..FormatTime(Seconds)
	elseif(Precision == "M")then
		return FormatTime(Minutes)
	elseif(Precision == "S")then
		return Seconds
	else
		warn("Invalid Precision Format: "..tostring(Precision))
	end
end

return TimeModule

It can handle Seconds, Minutes, and Hours.

1 Like

As someone who is new to all this (been doing it for about 2 weeks), how would I implement this into the script?

Any explanations would be helpful, I want to understand how it works rather than just copy paste. If the Wiki had a good explanation I would look there but nothing I have seen helped.

I am still diving into ModuleScripts so I am not sure if I will understand, but I will definitely take a look

Obligatory I believe this should work but if it doesn’t let me know of any errors.

local RoundLength = 5
local IntermissionLength = 50
local InRound = game.ReplicatedStorage:FindFirstChild("Remotes").InRound
local Status = game.ReplicatedStorage:FindFirstChild("Remotes").Status

local function GetTime(Time)
	local min, sec = tostring(math.floor(Time/60)), tostring(Time%60)
	return string.format('%f:%02f', min, sec)
end

local function roundTimer()
	while wait() do
		do
			local TempTime = IntermissionLength
			repeat wait(1)
				InRound.Value = false
				Status.Value = GetTime(TempTime)
				TempTime = TempTime - 1
			until TempTime <= 0
		end
		do
			local TempTime = IntermissionLength
			repeat wait(1)
				InRound.Value = true
				Status.Value = GetTime(TempTime)
				TempTime = TempTime - 1
			until TempTime <= 0
		end
	end
end

Edit: Copy and pasted an extra line that set the Time inside of the “GetTime” function, sorry about that.

1 Like

No errors, nothing works at all.

I checked to see where it was erroring and I couldn’t see, the Values aren’t updating which is the only indicator I have.

I am a goofball and just realized that I had the localscript for the function in the StarterGui disabled, the rest of the script works which means the script is reading times, but it seems to only be teleporting me based off of IntermissionLength and not RoundLength, as well as it’s not displaying the times in the UI above the screen.

I have no errors as well.

Since nothing in my code snippet is related to teleporting or displaying time, that’d be an error on your end and without the code I am unable to fix it.

Do you have a Discord I could contact you on? Also, I really appreciate all your help

Wyatt#3928

30 characters