[Old Post] Check how many minutes there are in seconds

[This post is old, nothing here reflects the way i program today]

I need help with a script that counts the seconds, i have a counter but I want to make it, for example, instead of appearing 60 in the text, it would appear 1:00 and that the counter continues to do his job, count

  1. Script
local RE = RS.Events.AskButton

RE.OnServerEvent:Connect(function(player)
	wait(1)
	script.Parent.Text = game.ServerStorage.GameConfig.RoundConfig.Timer.Text
	--script.Parent.Parent.Visible = true
	while true do
		wait (1)
		if script.Parent.Text == "0" then return end
		script.Parent.Text = script.Parent.Text -1
		
	end
end)
  1. Solutions i tried

I researched the developer hub many times, but I didn’t find anything like that

1 Like

just check if the text or int value or whatever you are using if it ever >= 60 then set the text to
“1:”…Number
or have 2 values for the timer. One is Minutes and one is seconds. If the seconds ever == 60 then seconds -= 60 and minutes += 1

1 Like

This is the funny thing, this I have tried, but to do this, for example if it were 67 instead of just a minute, I would have to put the 1: and 7

hmm ok give me a bit and i should have a script for you if that is ok

1 Like

@TheCarbyneUniverse has an awesome time formatting module available for public use.
You can read more about it here.

Alright you may have to edit it for it to work for your GUI but make it a normal script in ServerScriptService and put 2 intvalues in the script name one “Minutes” and the other "Seconds

local name = "Put the name of your ScreenGUI here"

while script.Disabled ~= true do
	wait(1)
	script.Seconds.Value += 1
	if script.Seconds.Value >= 60 then
		script.Seconds.Value -= 60
		script.Minutes.Value += 1
	end
	for _, player in pairs(game.Players:GetPlayers()) do
		player.PlayerGui:WaitForChild(name)
		if script.Seconds.Value >= 10 then
			wait()
			player.PlayerGui[name].Timer.Text = script.Minutes.Value .. ":" .. script.Seconds.Value
		else
			wait()
			player.PlayerGui[name].Timer.Text = script.Minutes.Value .. ":0" .. script.Seconds.Value
		end
	end
	wait()
end
1 Like

Seriously, thank you very much for your help, but I think I already had an idea in my head after reading your code

the script dont work correctly on my game, but thank you very much again for writting it <3

This is how I do all my timers, using seconds, hours and minutes:

local Time = script.Parent.TimeFrame.Uptime

local CountUpToTime = 0

local TimerActive = false

while true do 
	
	wait(1)
	
	TimerActive = not TimerActive
	
	if TimerActive then
			
		CountUpToTime = CountUpToTime + 1
			
		local Seconds = CountUpToTime % 60 
		local Minutes = math.floor(CountUpToTime / 60) % 60 
		local Hours = math.floor(CountUpToTime / (60 * 60))
			
		if string.len(Seconds) < 2 then Seconds = "0" .. Seconds end 
		if string.len(Minutes) < 2 then Minutes = "0" .. Minutes end
		if string.len(Hours) < 2 then Hours = "0" .. Hours end
			
		local Format = Hours .. ":" .. Minutes .. ":" .. Seconds
			
		Time.Text = Format
	end
end
2 Likes

Not sure what you mean by that. You can call a function in a modulescript as many times as you want, and from multiple different scripts.

1 Like

This will create a formatted 00:00:00 timer string from a tick() - startTick value:

local function formatTime(ellapsedTime)
	
	local centiseconds = ellapsedTime - math.floor(ellapsedTime)
	local seconds = ellapsedTime - centiseconds
	local minutes = 0
	
	if seconds >= 60 then
		minutes = math.floor(seconds / 60)
		seconds = seconds % 60
	end
	
	local minutesFmt = string.format('%02d', minutes)
	local secondsFmt = string.format('%02d', seconds)
	local centisecondsFmt = string.format('%02d', (centiseconds * 100) / (100 / 60)) 
	
	local timerFmt = minutesFmt .. ":" .. secondsFmt .. ":" .. centisecondsFmt
	
	return timerFmt

end

Then you could use like this:

local timerStart = tick()
local INTERVAL   = 0.02
local nextStep   = tick() + INTERVAL
local lastTime   = 0

game:GetService("Heartbeat"):Connect(function()
	if (tick() >= nextStep) then
		nextStep = nextStep + INTERVAL
		local ellapsedTime = string.format('%.2f', tick() - timerStart)
		local timerFmt = formatTime(ellapsedTime)		
		if timerFmt ~= lastTime then
			print(timerFmt)
			lastTime = timerFmt
		end
	end
end)

This will do 60th of seconds so you can also just to minutesFmt and secondsFmt if you want to keep it simple.

1 Like

This is quite an invalid script. You’re trying to perform an arithmetic function (subtraction) on both an instance and an int which won’t really work. And you’re using a while true do loop for countdowns which, there is a better way.


local duration = 5 * 60 --multiply minute by 60 to get total seconds of minutes

for i = duration, 0, -1 do
     local minutes = math.floor(i/60) -- divide variable i by 60 
     local seconds = math.floor(i%60); -- divide variable i by 60 and use % to get remainder for seconds

     if (minutes < 10) then
          if (seconds < 10) then
               script.Parent.Text == "0"..minutes..":".."0"..seconds;
          else
              script.Parent.Text == "0"..minutes..":"..seconds;
          end
     else
          if (seconds < 10) then
               script.Parent.Text == minutes..":".."0"..seconds;
          else
               script.Parent.Text == minutes..":"..seconds
          end
     end

     wait(1);
end

After this, you won’t have to worry about checking if the text is 0. The loop automatically stops once it reaches either 0 or -1, and stops yielding the entire script (although it’s recommended to do wrap this in a coroutine).

EXPLANATION

Numeric For Loop (click to expand)

Now, what did I do here? I used a numeric for loop

(Documentation about it can be found here.)

The For loop in Lua has two variants: numeric and generic. The generic one is for usually iterating over dictionaries and arrays, the numeric one is for repeating a task (usually) for a specified amount.

It can be in any form, either counting down (like what I did above), or the normal one. The documentation I linked would do a great job at explaining it more than I do.

math.floor

math.floor basically converts a float into an int, or lowers it to the closes integer.

If you don’t know what integers (ints for short) are, they’re basically whole numbers. Not anything like 3.14, 5.56, 0.1166777, just 1, 4, and more. Floats are the exact opposite of ints, and if you like to be fancy, they are called “floating points”.

What math.floor does is basically lower the float to the closest integer. For example, let’s say we have 5.6 or 5.7, if we use math.floor on that it’ll return 5, removing the dot and the additional fraction.

There is also another math function that is the exact opposite of math.floor, which is math.ceil. Which returns the highest integer closest to the floating point (e.g. 5.4 > 6).

Percentage symbol?

As we all know, dividing a number by 60 gives us the minutes. For example, there are 300 seconds in 5 minutes. If we divide 300 by 60, we get 5, which is the minute. But what about %?

% is called “Modulus”, it is highly similar with division (/), but instead of returning the result, it gives us the remainder, which, if we use this, it gives us the seconds, but capped to 60/59 (since we don’t want the counter’s seconds to go over like 04:304).