Simple Countdown Script

Hey Developers!

I am trying to make a countdown loop script where the text would repeat by saying "Get $5 in 120 seconds* and the number value of 120 only goes down and when it reaches 0, it goes back to 120.

I just need help as to what is the easier way to make the time decrease without having to keep changing the text 120 times in a script because that’s what I was doing. I know there is a way, but I just need some explanation so my mind doesn’t stumble on this again!

image

Thoughts?

1 Like

Use a for loop like so:

for i = 120, 0, -1 do
    PATH_TO_TEXT = i
    wait(1)
end
6 Likes

script.Parent.Text = ("Get $5 in 120 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 119 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 118 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 117 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 116 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 115 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 114 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 113 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 112 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 111 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 110 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 109 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 108 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 107 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 106 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 105 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 104 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 103 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 102 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 101 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 100 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 99 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 98 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 97 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 96 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 95 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 94 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 93 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 92 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 91 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 90 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 89 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 88 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 87 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 86 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 85 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 84 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 83 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 82 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 81 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 80 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 79 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 78 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 77 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 76 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 75 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 74 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 73 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 72 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 71 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 70 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 69 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 68 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 67 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 66 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 65 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 64 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 63 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 62 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 61 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 60 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 59 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 58 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 57 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 56 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 55 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 54 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 53 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 52 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 51 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 50 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 49 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 48 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 47 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 46 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 45 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 44 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 43 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 42 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 41 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 40 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 39 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 38 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 37 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 36 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 35 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 34 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 33 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 32 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 31 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 30 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 29 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 28 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 27 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 26 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 25 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 24 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 23 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 22 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 21 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 20 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 19 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 18 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 17 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 16 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 15 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 14 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 13 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 12 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 11 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 10 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 9 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 8 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 7 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 6 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 5 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 4 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 3 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 2 seconds")

wait(1)

script.Parent.Text = ("Get $5 in 1 second")

wait(1)

script.Parent.Text = ("Get $5 in 0 seconds")

wait(1)

end```
14 Likes

This is what I have right now, how would I structure it in your loop?

Would be much more efficient to use a loop. To make the loop fit your format, you’d simply do:

for i = 120, 0, -1 do
    script.Parent.Text = "Get $5 in "..i.." seconds"
    wait(1)
end
6 Likes

You definitely do not want to do it like this, it makes everything hard to read down the line. The cool thing is the same thing can be done in 4 lines! :slight_smile:

for i = 120, 0, -1 do
	script.Parent.Text = string.format("Get $5 in %i Seconds", i)
	wait(1)
end

edit: made a typo, fixed that!

6 Likes

Hey thank you so much! May I ask the simple explanation of the line of code you wrote and what it does line by line? :slight_smile:

2 Likes

I think this is great, although it’s basic! I would recommend moving this to #help-and-feedback:cool-creations if you are asking for peoples opinions on this. I genuinely see that you also need scripting support though! Great job!! :smiley:

You can use a for loop instead.

for i = 120, 1, -1 do
   script.Parent.Text = "Get $5 in " .. i .. " seconds"
   wait(1)
end
2 Likes

Of course!

for i = 120, 0, -1 do
This line counts down from 120 to 0 in increments of -1. This number is denoted by i.

script.Parent.Text = string.format("Get $5 in %i Seconds", i)
This line sets the parents text to a formatted string! Formatted strings allow you to sub in values. For instance, here we use %i to denote that we are going to be placing an integer here. The second argument is what is placed from left to right. In this case, we only have %i, therefore we only need i to take its place :slight_smile:

Now, if i did this:
string.format("Hello my name is %s and I am %i years old!", "John Doe", 9000)
It would place “John Doe” in %s’s place, and 9000 in %i’s place.

Here is some documentation on the different specifiers.

hopefully this helps! Glad to see your eger to learn!

4 Likes

If no other ideas work make a number value called Countdown in players and then put a script or local script somewhere for example. I assume you know how to reset it and other stuff to do for it.

Blockquote

local CountValue = game.Players.Countdown.Value

while wait(1) do
CountValue = CountValue - 1
end

Blockquote

Then put a local script in the countdown text and make the code

Blockquote

while wait(0.01) do
script.Parent.Text = (game.Players.Countdown.Value)
end

Blockquote

There should be no errors unless I typed it wrong on my phone or you typed/copied wrong. I hope you understand script because it’s simple and that it works and I gtg now byee

i really prefer using for loops instead, it is faster way and either a better way