Breaking a loop with a function

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

Any help would be greatly appreciated! :+1:

2 Likes

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.

2 Likes

you can use couroutines and couroutine.yield() and coroutine.resume()
and alsot there’s something called ‘break’ which stops a loop

2 Likes
canLoop = true


while wait() and canLoop do
	camera.CFrame = Camera1.CFrame
	Animation1:Play()
	Animation1.Completed:Wait()
	camera.CFrame = Camera5.CFrame
	Animation3:Play()
	Animation3.Completed:Wait()
end


button.MouseClick:Connect(function()
canLoop = false
Animation1:Stop()
Animation3:Stop()
camera.CFrame = workspace.CameraE1.CFrame
end)
4 Likes

This wouldn’t break the loop this wouldnt run it

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)
3 Likes

that does the same thing
30char

1 Like

No, one breaks it and one prevents it from running as returning false when canloop ~= true

1 Like

Except it clearly shows a separate function, so the click Connect can actually happen.

1 Like

I’ll try to use cortoutines. I’ll let you know if it worked or not :+1:

it runs while canLoop == true, as soon as its false, it breaks.

Don’t while loops only break when the condition is met when the loop starts, not during the loop?

I’m still trying to understand how the system works :stuck_out_tongue:

It doesn’t breaks!!
it’s just stops working doesnt means it breaks

the opposite, it will run as soon as the script starts, and while canLoop == true, it will run. As soon as its false, it will stop

The logic is equivalent. A while check is checked on every iteration.

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