Creating a Clock Timer

I’m interested in making a timer that counts down. Usually when I make a timer for something like a round-script, it just looks like:

for i = 5,0,-1 do
 script.Parent.Text = "Intermission: " .. i
end

Something like that, but I’m trying to make a clock style timer. So instead of saying 60 seconds it would say 1:00… The thing is, I have no idea where to start. I’ve searched up tutorials and looked for similar posts, but couldn’t find anything that could help me. If you have any ideas how I could go about doing this let me know!

10 Likes

You can use this to convert an int to minutes.

script.Parent.Text = "Intermission: " .. tostring(math.floor(i/60))..":"..tostring(math.floor(i%60))
18 Likes

I assume the t is suppose to be an i ? [after the first math.floor]

1 Like

Correct, I was assuming t for time, it is edited now.

4 Likes

It works but when it goes under 10 seconds, it looks like: 0:4 instead of 0:04. Any ideas how to fix?

4 Likes

Never mind I found a solution. For anyone who sees this post in the future. Here is my script:

		for i = 15, 0,-1 do
			local minute = tostring(math.floor(i/60))
			local sec = math.floor(i%60)
			if sec < 10 then
				sec = "0".. tostring(math.floor(i%60))
			end
			status("Intermission: " .. minute .. ":" .. sec)
			wait(1)
		end
17 Likes

I was going to say to concat another zero if it’s under 10, but looks like you solved that problem.

3 Likes

Just saying; there’s a better way to handle your seconds. I want to introduce you to a cool string pattern that I learned the other day, %.2i. This pattern will insert a 0 as the first number if the number is below 9. Other ways to write this are %.2d, %02d and %02i. You can also replace 2 with 3 so it’s under 99 for a 0 to be added, so on.

Here’s some code that will automatically insert that awesome 0 for you. Before I post that, I’d like to point out that your concatenation could’ve used the calculated sec value instead of doing it again.

for i = 15, 0, -1 do
    local minutes = math.floor(i/60)
    local seconds = math.floor(i%60)
    status(string.format("Intermission: %i:%.2i", minutes, seconds))
    wait(1)
end

cc @ThousandDegreeKnife

23 Likes

I will give this a try! Thank you! :slight_smile:

1 Like

Hey! What’s the status? When I try to use it creates a error between that.

1 Like

that was 2 years ago lol

dont bumb things that dont need to be

1 Like

The response I created here was based off of what OP originally resolved their question with which involved a status function of some kind. My code was only intended to improve on theirs. Presumably status is a function that’s meant to update a value of some kind somewhere.

You can write your own status code. The main idea, if you’re looking to my post for formatting numbers, should be everything except the status function so that means the string formatting and calculations done to retrieve the time. It is a pretty simple piece of code but a little old and missing some parts like some subtractions but it still gets the job done.

@AC_Starmarine It’s only a bump if the post doesn’t contribute to the thread. Asking a question is fine, leave it be. Flag a post if you believe it doesn’t belong rather than calling someone out for it.

1 Like

Alright! Thank you. I was just scrolling through things to see if I can get an Idea of a code that will fix my problem as well with the format of the Numbers (0:0).

thx for the reply, you heled me a lot

i mean helped me a lot. Thx again

1 Like

it worked thanks man greatly appreciated evem if i didnt create the post

damn this is old but status is underlined? the status("intermission:

Whats the status? It underline sit in red, and im very confused.

Status is a function that the person made to set the text of a certain label to the passed value

How do I make it so it loops whenever I want it to again?