Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Making a countdown function?

So basically, I was trying to make a countdown function for my game. Here is what one of my friends came up with:

for countDown = 10, 0, -1 do
	if countDown > 9 then
		timeValue.Value = '0:' ..countDown
	else 
		timeValue.Value = '0:0' ..countDown
		wait(1)	
	end	
end

I was wondering if there was a better method for doing this. If there is could any of you possible show me how to do it?

PS. I am trying to make it look like this: 0:10, 0:09, 0:08. etc.

2 answers

That's how most other people would do it, but I'd move the wait(1) so it happens on every iteration.

for countDown = 10, 0, -1 do
    if countDown > 9 then
        timeValue.Value = '0:' ..countDown
    else 
        timeValue.Value = '0:0' ..countDown
    end 
	wait(1) --This way, it wait()s regardless of whether or not countDown is greater than 9
end

This is the best way you could do this, but if you'd like a wiki to better understand what is going on in this code, you can go here: Loop Wiki. Basically, this code you provided will make a variable named "countDown". This variable will start at 10, and subtract one every second until the variable is 0. The reason the if statement is there is so if the variable is below 9, it will show it as 0:0 (variable). Otherwise, if the variable is above 9, it will be displayed as 0: (variable). But there are a few problems in your code. Let me fix them for you.

for countDown = 10, 0, -1 do
    if countDown <= 9 then --You need to add a "=" here, so it checks if it is below or equal to 9. Also, you need to make it "<", so it checks if it's less than, not greater than.
        timeValue.Value = '0:' ..countDown
    else 
        timeValue.Value = '0:0' ..countDown 
    end
    wait(1) --you should move the "wait(1)" down to here, so it will remove one every second more efficiently.
end

2 comments

0

Duplicate Answer.

0

I added on to the answer above, also he didnt fix a problem the code had provided, which I fixed in my answer.

This discussion is preserved as it was. New answers, comments, and votes are closed.