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

How to make multiple while true do loops?

Asked by
trecept 367 Moderation Voter
6 years ago

I wasn't sure how to phrase this question, basically I made a button to add 1 to "num", but whenever I try to script after a while true do loop it wont work.

a = 0
adding = false
b = 0
adding2 = false

button.MouseButton1Click:connect(function()
adding = true
end)
button2.MouseButton1Click:connect(function()
adding2 = true
end)

while true do
wait(0.5)
a = a + 1
end
while true do
wait(0.5)
b = b + 1
end

The second while true do loop wont ever run, and I was going to use runservice stepped but I can't use wait() there. I tried combining the loops into one but it was like adding them slower and differently, I'm not sure if I did something wrong. Thanks!

0
a loop goes on forever and until the loop stopped, you can't run the next lines of code greatneil80 2647 — 6y

2 answers

Log in to vote
0
Answered by
Vulkarin 581 Moderation Voter
6 years ago

The fix to your script is to just combine the loops, there isn't any reason to have two seperate but identical loops

while true do
    wait(0.5)
    a = a + 1
    b = b + 1
end

As for why it didn't work.. Scripts run sequentially from top to bottom, when you put your original while true do loop the script is stuck endlessly doing this loop, it will not continue the remainder of the script below the loop until the loop is escaped from

Ad
Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

You can just put them in the same loop with additional logic. Something like this:-

while adding or adding2 do -- note this loop will end when both value are false
    if adding then
        a = a + 1
    end
    if adding2 then
        b = b + 1
    end
end

You could also use spawn example code included in the wiki page.

A better solution would to simply use time though I do not know how you intend to use these variables.

I hope this helps.

Answer this question