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

How do i break my loop after a gui is clicked?

ScriptService

local remote = game.ReplicatedStorage.Menu

game.Players.PlayerAdded:Connect(function(plr)
	remote:FireClient(plr)
end)

StarterGui

local cc = game.Workspace.CurrentCamera
local playcam1 = game.Workspace.Camera1
local playcam2 = game.Workspace.Camera2
local playcam3 = game.Workspace.Camera3
local playcam4 = game.Workspace.Camera4
local player = game.Players.LocalPlayer
local remote = game.ReplicatedStorage.Menu
local playbutton =  script.Parent.Play

function MainMenu()
	repeat
		cc.CameraType = Enum.CameraType.Scriptable
		wait(1)
	until cc.CameraType == Enum.CameraType.Scriptable
	playbutton.Visible = true
        while true do
        wait()
	cc.CFrame = playcam1.CFrame
	wait(5)
	cc.CFrame = playcam2.CFrame
	wait(5)
	cc.CFrame = playcam3.CFrame
	wait(5)
	cc.CFrame = playcam4.CFrame
	wait(5)
	end
	
	remote.OnClientEvent:Connect(function()
		MainMenu()
end)

playbutton.MouseButton1Click:Connect(function()
	playbutton.Visible = false
	cc.CameraType =  Enum.CameraType.Custom
end)

1 answer

✓ Accepted answer

You can use coroutines to start and end loops. Note: This is just an example, don't copy this directly to your code.

local debounce = false
local Button = script.Parent

local loop = coroutine.create(function(message) -- creates the loop
	while true do
		print(message)
		task.wait()
	end
end)

Button.MouseButton1Click:Connect(function() -- when the button is clicked
	if debounce == false then
		debounce = true
		coroutine.resume(loop, "hi") -- starts the loop; will print "hi" multiple times
	else
		debounce = false
		coroutine.close(loop) -- ends/breaks the loop, making it unresumable again unless redefined
		
		loop = coroutine.create(function(message) -- redefines; creates the loop again
			while true do
				print(message)
				task.wait()
			end
		end)
	end
end)

1 comment

0

Nice! thanks for telling me this, I'll see if it works

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