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

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

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

[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

5 comments

1

Will do!

0

:)

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?

0

Fixed it! Now the code definitely works!

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 :)

u should probably use a while loop

function aa(toggled)
  	 while toggled do
		wait()

	end
end

1 comment

0

What about checking if it's not toggled?

try this:

function aa(toggled)
	repeat
	print("aa")
	wait()
	until toggled == false
	print("a")
end

1 comment

0

Is there any way I can do this with a RemoteEvent for my auto clicker for my game?

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

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