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 break this loop once the boolean value changes?

Hey eveyone, sorry for the dumb question but i am having some difficulties right now with my code, I want to have a loop break once the boolean value updates, here is the script:

local MenuOver = script.Parent:WaitForChild("MenuOver")
MenuOver.Value = false
print("Menu Over is ", MenuOver.Value) -- This prints successfully
PlayBtn.MouseButton1Click:Connect(function()
	MenuOver.Value = true
	SlideInSide:Play()
	print("Menu Over is ", MenuOver.Value) -- This also prints correctly
end)

Here is the issue here, i want the loop here to break once the value changes, how do i do that?

while MenuOver.Value == false do
	Cam.CFrame = CamPoints:FindFirstChild("1").CFrame
	TweenCameraPos(CamPoints:FindFirstChild("2"), 4)
	wait(4)
	Cam.CFrame = CamPoints:FindFirstChild("3").CFrame
	TweenCameraPos(CamPoints:FindFirstChild("4"), 4)
	wait(4)
	Cam.CFrame = CamPoints:FindFirstChild("5").CFrame
	TweenCameraPos(CamPoints:FindFirstChild("6"), 4)
	wait(4)
	Cam.CFrame = CamPoints:FindFirstChild("7").CFrame
	TweenCameraPos(CamPoints:FindFirstChild("8"), 4)
	wait(4)
	Cam.CFrame = CamPoints:FindFirstChild("9").CFrame
	TweenCameraPos(CamPoints:FindFirstChild("10"), 4)
	wait(4)
	if MenuOver.Value == true then
		Cam.CameraType = Enum.CameraType.Follow
        Cam.FieldOfView = 70
        break
	end
end

Thanks!

1 answer

✓ Accepted answer

Well, the first thing you'd want to do would to be not to repeat your code more than necessary, but after that, you'd just want to check in between every step whether it met the conditions. Also, you might want to have some conditions in place in case FindFirstChild doesn't find what it's looking for.

while MenuOver.Value == false do
	for i=1,10 do
		if MenuOver.Value == true then
			--Lets break out of the loop here
			break
		end

		if i % 2 == 1 then
			--Odd number, update the CFrame
			Cam.CFrame = CamPoints:FindFirstChild(tostring(i)).CFrame
		else
			--Even number, tween the camera
			TweenCameraPos(CamPoints:FindFirstChild(tostring(i)), 4)
			wait(4)
		end
	end
end
Cam.CameraType = Enum.CameraType.Follow
Cam.FieldOfView = 70

4 comments

0

It worked! Thanks, can you help me understand what this code does?

0

What I did is I made a for loop that went through every number 1 to 10 instead of writing out 1,2,3,4,5,6,7,8,9,10, the script does that with the for loop.

0

Don't use FindFirstChild and assume it returns something because it doesn't always return something.

0

This isn't using FFC's second argument as a number. It's using TweenCameraPos's second argument as a number, I just rewrote his code and did what he wanted.

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