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

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

Asked by
BIGM_7 4
2 years ago

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

Log in to vote
0
Answered by 2 years ago

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)
0
Nice! thanks for telling me this, I'll see if it works BIGM_7 4 — 2y
Ad

Answer this question