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

Question about while loops?

Asked by
ded_srs 30
4 years ago
Edited 4 years ago
    while i < 5 do
        wait(0.5)
        boot_text.Text = boot_text.Text .. " ."
        i = i + 1 
        if i == 5 then
            i = 1 
            boot_text.Text = "Booting"
        end
    end

I want this loop to stop after 3 cycles (meaning that once i has been reset to = 1, three times).

Is this possible?

0
do you just want it to run 3 times? soccerstardance251 29 — 4y

2 answers

Log in to vote
0
Answered by
Ziffixture 6913 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

You can use a nested for loop to achieve this. Essentially, we have a crown loop call a second loop #n time(s). You can also achieve better efficiency with this iteration function as it allows us to provide a numerical condition start, stop, step (optional) which get's pushed into a parameter that we can use to keep track of our current iteration in the same behavior as creating a variable and incrementing it.


Note:

You can use the :rep() method of String to determine how many trailing dot characters you wish to append to the text without having to add it piece by piece.


for _ = 1,3 do
    for i = 0,5 do
        boot_text.Text = "Booting"..('.'):rep(i)
    wait(.5) end
end
0
Thank you! ded_srs 30 — 4y
Ad
Log in to vote
1
Answered by 4 years ago
Edited 4 years ago

Yes this is possible. Maybe the simplest way would be to add a cycle variable to count how many times the loop runs. I will insert this variable into your code. Actually let me adjust the code a little bit more.

local cycleCount = 0
while cycleCount < 3 do
    cycleCount = cycleCount + 1
    for i = 1, 5 do
        wait(0.5)
        boot_text.Text = boot_text.Text .. " ."
        i = i + 1 
        if i == 5 then
            i = 1 
            boot_text.Text = "Booting"
        end
    end
end

This should just count each time the while loop runs and stop when cycleCount gets to 3. It should also run the loop to get i to 5 and run the code for that as well. Hopefully this helped!

0
I just updated the answer given. Hopefully this is what you wanted it to do, I have not tested it but it should give you an idea. Nitro989 5 — 4y
0
Use a nested for loop. Ziffixture 6913 — 4y
0
You could also use a nested for loop yes. Nitro989 5 — 4y
0
Thank you! ded_srs 30 — 4y

Answer this question