How to break out of a cutscene while do loop when a button is pressed?

Hey there, this is my first post on the forums.

I am currently wanting one of my main menu play buttons to “break” a while do loop for a cut scene I have in my game once it is clicked. However, I am having trouble with understanding how to do that as I am novice in scripting.

One local script is responsible for the cut scene (a camera which rotates around the map) and the other is the play button (once pressed it removes the main menu GUI and stops the cut scene).

One option I have tried is adding an if statement which checks if the frame is disabled in order to break the loop.

Edit*: The cameras 4 cameras I have are named: Test1, Test2, Test3 and Test4.

Here is the local script for the cut scene:

local TweenService = game:GetService("TweenService") --Getting tweenservice
local camera = game.Workspace.Camera --variable for camera
local cutsceneTime = 20 --length of cutscene
local frame = game.StarterGui.MainMenu

local tweenInfo = TweenInfo.new( 
	cutsceneTime, --time
	Enum.EasingStyle.Linear, --style
	Enum.EasingDirection.Out, --direction
	0, -- repeat
	false, -- reverse
	0 -- delay
)

function tween(part1,part2)
	camera.CameraType = Enum.CameraType.Scriptable --allows to edit camera
	camera.CFrame = part1.CFrame --position and rotation, looks from the position of part 1

	local tween = TweenService:Create(camera, tweenInfo, {CFrame = part2.CFrame})
	tween:Play()
	
	wait(cutsceneTime)
	
	camera.CameraType = Enum.CameraType.Custom
end

while true do
	wait()
	tween(game.Workspace.Test1,game.Workspace.Test2)
	tween(game.Workspace.Test2,game.Workspace.Test3)
	tween(game.Workspace.Test3,game.Workspace.Test4)
	tween(game.Workspace.Test4,game.Workspace.Test1)
	if frame.Enabled == false then
		break
	end
end

And here is the local script for the button:

local button = script.Parent
local frame = script.Parent.Parent.Parent.Parent.Parent.MainMenu
local player = game.Players.LocalPlayer

button.MouseButton1Click:Connect(function()
	frame.Enabled = false
end)

Here is a screencap of my studio:

Kind regards,
ricefield_man

1 Like

If I would do it, I would have done something like this:

local PressedPlay = false

while not PressedPlay  do
	wait()
	tween(game.Workspace.Test1,game.Workspace.Test2)
	tween(game.Workspace.Test2,game.Workspace.Test3)
	tween(game.Workspace.Test3,game.Workspace.Test4)
	tween(game.Workspace.Test4,game.Workspace.Test1)
end

Then just change PressedPlay when play button is pressed.

local TweenService = game:GetService("TweenService") --Getting tweenservice
local camera = game.Workspace.Camera --variable for camera
local cutsceneTime = 1 --length of cutscene

local tweenInfo = TweenInfo.new( 
	cutsceneTime, --time
	Enum.EasingStyle.Linear, --style
	Enum.EasingDirection.Out, --direction
	0, -- repeat
	false, -- reverse
	0 -- delay
)

function tween(part1,part2)
	camera.CameraType = Enum.CameraType.Scriptable --allows to edit camera
	camera.CFrame = part1.CFrame --position and rotation, looks from the position of part 1

	local tween = TweenService:Create(camera, tweenInfo, {CFrame = part2.CFrame})
	tween:Play()
	
	wait(cutsceneTime)
	
	camera.CameraType = Enum.CameraType.Custom
end

local playButton = game.StarterGui.MainMenu.Main.Buttons.PlayButton
local PressedPlay = false

playButton.MouseButton1Click:Connect(function()
	PressedPlay = true
end)


while not PressedPlay do
	wait()
	tween(game.Workspace.Test1,game.Workspace.Test2)
	tween(game.Workspace.Test2,game.Workspace.Test3)
	tween(game.Workspace.Test3,game.Workspace.Test4)
	tween(game.Workspace.Test4,game.Workspace.Test1)
end

Apparently it still keeps playing the loop even after I’ve pressed the play button.
big rip. :frowning:

Well, you set the cutsceneTime to 1, you should destroy the loop using Instance:Destroy

I might just make it a static camera then instead of a looping camera.
This is like hell for me lol.

However, thank you for your help guys!

No problem, if you need anything just make a post and we will try and help you!

1 Like

Many thanks, I thought it’d be scary to post on here cause I thought most scripters are very “strict” in a way lol. Thank you again!

1 Like