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

Repeat loop doesn't stop when toggled is equal to false?

Asked by 3 years ago

I'm trying to make a loop that prints "aa" when the gui is toggled and when it's not it will print "a" once, but even after I turn off toggle it keeps on going

function aa(toggled)
   if toggled then
       repeat wait()
           print("aa")
       until not toggled
       else
           print("a")
           return
     end
end

4 answers

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

[EDIT] The execution was stuck in a loop at the beginning that it didn't execute the listener event.

Here is the working code:

local button = script.Parent
local toggle = true

script.Parent.ClickDetector.MouseClick:Connect(function()
    toggle = not toggle
    if toggle then aa() end
end)

function aa()
    while toggle do
        print("aa")
        wait()
    end
    print("a")
end

aa() -- Start the function for the first time
1
Will do! TheNINJA152 85 — 3y
0
:) nikoviking 236 — 3y
0
It still keeps on printing, also I've wanted to make the loop constantly fire a RemoteEvent for an autoclicker in my game, but it keeps on going when I click the button to turn it off, do you know what I should do? TheNINJA152 85 — 3y
0
Fixed it! Now the code definitely works! nikoviking 236 — 3y
0
Did another edit, because I realised you wanted it to only print 'a' once instead of until the button is pressed again. Now the script should print "aa" constantly until you click the button, after which it will print "a" once. Pressing the button again will keep toggling on and off :) nikoviking 236 — 3y
Ad
Log in to vote
0
Answered by
IcyMizu 122
3 years ago

u should probably use a while loop

function aa(toggled)
     while toggled do
        wait()

    end
end
0
What about checking if it's not toggled? TheNINJA152 85 — 3y
Log in to vote
0
Answered by 3 years ago

try this:

function aa(toggled)
    repeat
    print("aa")
    wait()
    until toggled == false
    print("a")
end
0
Is there any way I can do this with a RemoteEvent for my auto clicker for my game? TheNINJA152 85 — 3y
Log in to vote
-1
Answered by 3 years ago

create a new script and remove the loop in it then hen you want it to be fired disable the first script and enable the other one

Answer this question