Before you tell me that there has already been a topic posted on this, please note that i’m trying to break the loop AS SOON as the player clicks on a button, which is why i was trying to use a function instead of an if statement. I know you can’t break a loop with a function, so if anybody knows how to break the loop as soon as the button is pressed, please leave a reply.
Here’s the code:
local function MainMenuCamera()
while wait()do
script.Parent.MouseButton1Click:Connect(function()
Animation1:Cancel()
Animation3:Cancel()
camera.CFrame = workspace.CameraE1.CFrame
break
end)
camera.CFrame = Camera1.CFrame
Animation1:Play()
Animation1.Completed:Wait()
camera.CFrame = Camera5.CFrame
Animation3:Play()
Animation3.Completed:Wait()
end
end
To be honest, I don’t like it when MouseButton1Click gets defined/created within a loop, which means that there would be lots of connections to it.
My suggestion is to take that connection outside of the while loop and create a new variable like local cancel = false. On the loop, you can have it check if it’s true, if it is, use break.
canLoop = true
local function MainMenuCamera()
while wait() do
if not canLoop then
break
end
camera.CFrame = Camera1.CFrame
Animation1:Play()
Animation1.Completed:Wait()
camera.CFrame = Camera5.CFrame
Animation3:Play()
Animation3.Completed:Wait()
end
end
button.MouseClick:Connect(function()
canLoop = false
end)
Jesus i’m so stupid. All this time i thought that’s how it worked. I might have gotten the logic mixed up with how if statements worked inside the actual while loop