Looping Main Menu Background Camera

First off, the animation looks really cool. wait() is deprecated; switch to task.wait() instead (the reason for it is explained here). pairs is not essentially deprecated, it’s just not necessary to implement the pairs iterator on the loop, because the loop automatically knows how’s the table syntax and iterates accordingly.

The final code would be something like that:

local TS = game:GetService("TweenService")

local orange = workspace.Camera 
local scenes = workspace.CameraScenes 

local currentTween
local looping = true

while not orange.CameraSubject do -- Changed to while loop because it first checks for the CameraSubject and if it is nil, loops through, differently from repeat, which doesn't check for the condition before entering the loop.
    task.wait()
end

orange.CameraType = Enum.CameraType.Scriptable

--Play button
script.Parent.MouseButton1Click:Connect(function() 
	orange.CameraType = Enum.CameraType.Custom

	looping = false

    if not currentTween then return end

	currentTween:Pause()
end)

--Scenes
while looping do
	for _, scene in scenes:GetChildren() do
		if not looping then return end

		orange.CFrame = scene["1"].CFrame 
		currentTween = tween:Create(orange, TweenInfo.new(10), {CFrame = scene["2"].CFrame})
		currentTween:Play()

		currentTween.Completed:Wait()
	end
end

EDIT: Removed the wait(10) because it’s more precise using the tween’s Completed event.

1 Like