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

How can I start and stop a loop using a button?

I would like to make a button which when clicked would start a loop, but then if clicked again would cause the loop to stop. How could I do this?

2 answers

✓ Accepted answer

Use the while loop.

Make sure there is a clickdetector and that its parent is the button.

I made the script for you:

local button = script.Parent
local clickdetector = button.ClickDetector
local toggled = false


clickdetector.MouseClick:connect(function()
	if toggled == false then
		toggled = true
		while toggled == true do
			--your script here
		end
	end
	if toggled == true then
		toggled = false
	end
end)

Use break. break is used for any type of loop, for loops, repeat-until loops, or while loops. break is self-explanatory, it breaks the loop.

local clickDetector = workspace.Part.Clickdetector
while wait() do
	clickDetector.MouseClick:Connect(function()
		break
	end)
end

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