Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How can I make this 30 second countdown more efficiant?

Asked by 4 years ago
Edited 4 years ago

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

Log in to vote
0
Answered by
Nckripted 580 Moderation Voter
4 years ago

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!

0
Sorry I forgot to put quotation mark at end of string lol! Nckripted 580 — 4y
0
This will make things so much more efficient, thank you so much! H0ney_Beeee 8 — 4y
Ad
Log in to vote
4
Answered by 4 years ago
Edited 4 years ago

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.

0
Thank you so much, this really helps! H0ney_Beeee 8 — 4y

Answer this question