How do I break my while true do loop as soon as the condition is met

The problem is that the if statement has to wait for the loop to restart to work, any work-arounds to this delay?

local camera = game.Workspace.CurrentCamera
local campos = game.Workspace.CameraPos
local player = game.Players.LocalPlayer
local tweenservice = game:GetService("TweenService")
local info = TweenInfo.new(12,Enum.EasingStyle.Linear, Enum.EasingDirection.InOut,0,false,0)
local titlemusic = script.Parent:WaitForChild("TitleMusic")
local pressplay = false
titlemusic:Play()
titlemusic.Looped = true

script.Parent.Play.MouseButton1Click:Connect(function()
	pressplay = true
	print("pressed play")
end)

while wait() do
	if pressplay == false then
		camera.CameraType = Enum.CameraType.Scriptable
		camera.CFrame = campos.Cam1.CFrame
		local tween = tweenservice:Create(camera,info,{CFrame = campos.Cam2.CFrame})
		tween:Play()
		tween.Completed:Wait()
		local tween1 = tweenservice:Create(camera,info,{CFrame = campos.Cam3.CFrame})
		tween1:Play()
		tween1.Completed:Wait()
		local tween2 = tweenservice:Create(camera,info,{CFrame = campos.Cam4.CFrame})
		tween2:Play()
		tween2.Completed:Wait()
		local tween3 = tweenservice:Create(camera,info,{CFrame = campos.Cam5.CFrame})
		tween3:Play()
		tween3.Completed:Wait()
		local tween4 = tweenservice:Create(camera,info,{CFrame = campos.Cam6.CFrame})
		tween4:Play()
		tween4.Completed:Wait()
		local tween5 = tweenservice:Create(camera,info,{CFrame = campos.Cam7.CFrame})
		tween5:Play()
		tween5.Completed:Wait()
		local tween6 = tweenservice:Create(camera,info,{CFrame = campos.Cam8.CFrame})
		tween6:Play()
		tween6.Completed:Wait()
		local tween7 = tweenservice:Create(camera,info,{CFrame = campos.Cam9.CFrame})
		tween7:Play()
		tween7.Completed:Wait()
		local tween8 = tweenservice:Create(camera,info,{CFrame = campos.Cam10.CFrame})
		tween8:Play()
		tween8.Completed:Wait()
		local tween9 = tweenservice:Create(camera,info,{CFrame = campos.Cam1.CFrame})
		tween9:Play()
		tween9.Completed:Wait()
	else
		print("break")
		break
	end
end


Well, it doesn’t always make sense to run it if your condition isn’t met then stop it in the middle but…
Not 100% sure, what I usually do though is add checks in between stuff, obviously you shouldn’t spam it but I usually do something like:

local shouldBreak = false
while wait() do
if shouldBreak == true then break end
-- Stuff
if shouldBreak == true then break end
--More stuff
end

Not the best but it works for me

i think the problem with that is the tween.completed:wait() would wait 12 seconds until the next should break check

From my knowledge, that’s the best way possible. There might be a better way, but I highly doubt it.

Okay I guess I was wrong, it is the best way lol

is there any other ways? the biggest problem is the tween delay, would i have to rewrite my code to get around that? if so can you tell me how i should?

You can use break keyword that are built-in Lua. Here’s the example of using it.

local n = 0;

while true do
   print('Current number is '..n);

   n += 1;

   if n > 10 then
      print('Loop break');
      break;
   end
end

The output should be like this

0
1
2
3
4
5
6
7
8
9
10
Loop break

I noticed that your coding style can be simplified:

local camera = game.Workspace.CurrentCamera
local campos = game.Workspace.CameraPos
local player = game.Players.LocalPlayer
local tweenservice = game:GetService("TweenService")
local info = TweenInfo.new(12,Enum.EasingStyle.Linear, Enum.EasingDirection.InOut,0,false,0)
local titlemusic = script.Parent:WaitForChild("TitleMusic")
local pressplay = false
titlemusic:Play()
titlemusic.Looped = true

local tweens = {
	tweenservice:Create(camera,info,{CFrame = campos.Cam2.CFrame}),
	tweenservice:Create(camera,info,{CFrame = campos.Cam3.CFrame}),
	tweenservice:Create(camera,info,{CFrame = campos.Cam4.CFrame}),
	tweenservice:Create(camera,info,{CFrame = campos.Cam5.CFrame}),
	tweenservice:Create(camera,info,{CFrame = campos.Cam6.CFrame}),
	tweenservice:Create(camera,info,{CFrame = campos.Cam7.CFrame}),
	tweenservice:Create(camera,info,{CFrame = campos.Cam8.CFrame}),
	tweenservice:Create(camera,info,{CFrame = campos.Cam9.CFrame}),
	tweenservice:Create(camera,info,{CFrame = campos.Cam10.CFrame}),
	tweenservice:Create(camera,info,{CFrame = campos.Cam1.CFrame})
}

script.Parent.Play.MouseButton1Click:Connect(function()
	pressplay = true
	print("pressed play")
end)

while wait() do
	if pressplay == false then
		camera.CameraType = Enum.CameraType.Scriptable
		camera.CFrame = campos.Cam1.CFrame
		for _, tween in pairs(tweens) do
			tween:Play()
			if pressplay == true then
				tween:Cancel()
				break
			end
			tween.Completed:Wait()
		end
	else break end
end

This allows you to actually check inbetween the tweens if pressplay is still true. This renders my other reply useless.

They already used break, they were asking for a better method.
Their usage is referenced here:

	else
		print("break")
		break
	end
end

Lua support double declaration using ;, so it should be like else print('break'); break; end

you are a saint, thank you so much, now i just gotta test it out

1 Like

You are very welcome. It took me a second to figure out it was possible.

I already removed that in my altered version. But yes, semicolons can be used.

I see a problem, the tweens just straight up won’t run, unless i change the

if pressplay == false then
	tween:Cancel()
	break
end

to

if pressplay == true then
	tween:Cancel()
	break
end

afterwards, when pressplay turns true it waits until the tween is completed to stop, any solutions?

1 Like

Oh, I forgot and didn’t notice that, but that’s the actual way to do it. I’ll edit the post.

I hope someone make a Moonscript support for Studio, since Moonscript has a feature to make conditional after declaring something.

print('Test') if true

( Also, Moonscript is just Ruby in Lua. So you may familiar with this thing. )

This can be done easily:

if true then print('Test') end

But, I get your point. If you want it to be added, try making a topic in Engine Features.

Wait! I got it! I’ll just turn camera into custom

1 Like

this post is officially solved