#While Condition Do Loops
You most commonly see them written as while true do. They loop until the condition is not true (false or nil). When the condition is not met, it'll break automatically.
while true do
print("Hi")
wait(1)
end
local a, b = 10, 13
while a < b do
a = a+1
b = b+0.5
end
#Coroutines
A coroutine is basically like creating a whole new script inside your script. That way, if you have any yields in your coroutine, it doesn't affect the rest of your script and the coroutine yields independent of whatever is going on in the rest of your actual script. How to make a coroutine:
local CountDown = coroutine.wrap(function()
for i = 10,0,-1 do
print("Countdown: "..i)
wait(1)
end
end)
CountDown()
wait(3)
print("Interruption :D")
Countdown: 10
Countdown: 9
Countdown: 8
Interruption :D
Countdown: 7
...
There are 2 waits in the script. Neither of them interrupt each other.
local activated = false
local MyLoop = coroutine.wrap(function()
activated = not activated
while activated do
wait(1)
end
end)
ToggleButton.MouseButton1Click:Connect(function()
MyLoop()
end)
Hope it helps!
5 comments
maybe try the break function
I dont know how to use the break function....
Try using separate scripts. When you press the button, it enables the script with the loop inside. And when clicked again, it'll disable the script therefor turning off the loop.
It needs to be on the same script...
add a open and close(true and false) variable so u can break or u can activate a loop