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

How can I make this 30 second countdown more efficiant?

I use countdowns a lot for things like intermissions, and it always works but I know there are much better ways to do it then how I do it, because the way that I do it is lengthy and kindof unreasonable. I'm just not exactly sure how. Help?

while true do
		script.Parent.Text = "30"
		wait(1)
		script.Parent.Text = "29"
		wait(1)
		script.Parent.Text = "28"
		wait(1)
		script.Parent.Text = "27"
		wait(1)
		script.Parent.Text = "26"
		wait(1)
		script.Parent.Text = "25"
		wait(1)
		script.Parent.Text = "24"
		wait(1)
		script.Parent.Text = "23"
		wait(1)
		script.Parent.Text = "22"
		wait(1)
		script.Parent.Text = "21"
		wait(1)`
end -- and so on

this is usually what I would use, and so on but there has to be a more efficient way of doing it. Thank you in advance!

2 answers

✓ Accepted answer

To add on to what Dovydas1118 said, you would want to make to keep track of players during a multiplayer game intermission, you can do something like this:

local continueScript = true

for i = 30,0,-1 do --Do zero because we don't want the countdown finishing at one
	local plrs = game.Players:GetChildren()

	if #plrs > 2 then
		wait(1)
	else
		continueScript = false
		break --Break out of the loop
	end
end

Then, for your main game code, do this:

if continueScript then
	--Game code
else
	print("Somebody left during intermission and we are waiting for players again
end

Hope this helped!

2 comments

0

Sorry I forgot to put quotation mark at end of string lol!

0

This will make things so much more efficient, thank you so much!

Use a for loop. A for loop is fine for a situation like this as it is in seconds, and not the usual minutes, seconds, etc.

for i = 30, 1, -1 do --counts down by 1 second
	script.Parent.Text = tostring(i) --I put this in a tostring just in case it breaks
	wait(1)
end

If you need to keep it on forever, then put a while true do at the top. Edit: I forgot to add a wait. My bad.

1 comment

0

Thank you so much, this really helps!

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