How do you turn seconds into minutes and seconds?

I’m trying to create a timer for my duel system and display it in min/sec such as 4:30. So how exactly would I turn 270 seconds into 4:30? I tried doing something similar but it did not work at all. So is there some kind of formula to do this?

 local min = 4
 local sec = 59 
 local secounds = 299

 repeat
    wait(1)
		 secounds -= 1 


		sec -= 1
		if sec < 1 then
			min -= 1
			sec = 59
		end


		script.Parent.Frame.TextLabel.Text = "Timer: " .. min .. ":" .. sec

until secounds == 0
1 Like

Divide the value by 60 and floor it (minutes) then mod the value by 60 to get seconds.

270/60 = 4.5 floored is 4.
270%60 = 30. (Percent sign is modulo)

if the value/60 < 1 then only show seconds

3 Likes
function SecondsToMMSS(Seconds)
	local SS = Seconds % 60
	local MM = (Seconds - SS) / 60 -- you could also do local MM = math.floor(Seconds / 60)
	return MM..":"..(10 > SS and "0"..SS or SS)
end

local seconds = 300

repeat
	script.Parent.Frame.TextLabel.Text = "Timer: "..SecondsToMMSS(seconds)
	seconds -= 1
	task.wait(1)
until seconds == 0

should work, looks like you only want minutes and second
I even tested it to make sure

10 Likes