Is it possible to stop a while true do loop?

Im trying to make a while true do loop stop using a script, and is that even possible?

3 Likes

Yes, using the break keyword. The break keyword can break any loop.

2 Likes

Do you mean like by using break? That would break the loop and stop it. But it would have to be inside the loop. You can go here for more information: Introduction to Scripting | Documentation - Roblox Creator Hub

1 Like

Just don’t. Use the intended use of a while loop instead and use the inverse(not) condition in the while instead of true only. It’s already overlooked.

2 Likes

If you need to do that outside of the loop, in another script, then you will also need a module script.
In the modulescript, initialise a key in the table to true, and return the table, you also need to require it in both server scripts.
Then, in the server script you have “while true do” in, you change the “while true do” to “while modulescript.keyOfTable do” (dont change to these exact names, it depends on where you store the table the modulescript returns, and what key did you put true in) , and in the other script, you change modulescript.keyOfTable to false when you need the while loop to stop.

You could use the break keyword or you could utilize the true or false condition that a while loop uses:

local counter = 0 
while counter ~= 3 do -- when this condition is false(when the counter = 3) the loop will stop
print("This code will run until counter is = 3")
counter = counter + 1 -- adding one to the counter after every loop


end
2 Likes

There are two ways to intentionally terminate a while loop, if the condition changes to what it’s specified to in the loop or if you use break.