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

How to make multiple while true do loops?

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!

1 comment

0

a loop goes on forever and until the loop stopped, you can't run the next lines of code

2 answers

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

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.

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