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

Is there a way I can make a toggle able loop?

Asked by 5 years ago
Edited 5 years ago

So, i'm trying to make a loop that starts when you click a button but, when you click that button again it stops. If you can help thanks!

ToggleButton.MouseButton1Click:Connect(function()
    --like the code here but I just can't think of anything that will make a
    --toggle able loop
end)

EDIT: Kind of solved.. gonna be practicing the examples that LordDragonZord told me

0
maybe try the break function theking48989987 2147 — 5y
0
I dont know how to use the break function.... GproGamerYT 5 — 5y
0
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. CaptainAlien132 225 — 5y
0
It needs to be on the same script... GproGamerYT 5 — 5y
0
add a open and close(true and false) variable so u can break or u can activate a loop HappyTimIsHim 652 — 5y

3 answers

Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

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.

--Loops until the condition is not true.
while true do --True will always be true; loops forever
    print("Hi")
    wait(1)
end

local a, b = 10, 13

while a < b do --Breaks once a is greater than b.
    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 --If false, it's now true (and vise versa)
    while activated do
        --Your Code Here
        wait(1) --A wait is necessary when you are looping many times.
    end
end)

ToggleButton.MouseButton1Click:Connect(function()
    MyLoop() --Calls the coroutine
end)


Hope it helps!

0
-_- greatneil80 2647 — 5y
0
^, wut User#23365 30 — 5y
Ad
Log in to vote
1
Answered by 5 years ago

Here's an idea I came up with.

local CanLoop = false 

ToggleButton.MouseButton1Click:Connect(function()
    if CanLoop then 
        CanLoop = false 
    else
        CanLoop = true 
    end
end)

while true do 
    wait()
    if CanLoop then 
        print("loop running")
    end
end
0
local CanLoop = false ToggleButton.MouseButton1Click:Connect(function() if CanLoop then CanLoop = false print('stopped') else CanLoop = true end end) while true do wait() if CanLoop then print("running") end end GproGamerYT 5 — 5y
Log in to vote
0
Answered by 5 years ago

To toggle anything ever. A two way toggle, there is this cool thing called a "Boolean" This boolean is a true or false value which can change depending on what you tell it to do.

To create a loop in an event depending on a bool, try this out!

local a = true
ToggleButton.MouseButton1Click:Connect(function()
    repeat wait()
    -- run code
    if a == true then
        -- you can make it false or run other code while a is true.
        -- if you insert a = false here then the loop stops.
    end
    until a == false
end)

If this helped, click the Answer Button C: Goodbye!

Answer this question