MainMenu Camera System

Hey there!

So for the main menu in my game i want for the camera to go to different scenes. I have this already set up however i’m having trouble adding another very important function.

When the player presses play, i want for the camera animations to pause (And then i can do the rest) This is what i have:

local camera = workspace.CurrentCamera

local Camera1 = workspace:WaitForChild("Camera1")

local Camera2 = workspace:WaitForChild("Camera2")

local Camera3 = workspace:WaitForChild("Camera3")

local Camera4 = workspace:WaitForChild("Camera4")

local TweenService = game:GetService("TweenService")

local TweenInformation = TweenInfo.new(4, Enum.EasingStyle.Linear, Enum.EasingDirection.In, 0, false, 0)

repeat wait()

camera.CameraType = Enum.CameraType.Scriptable

until camera.CameraType == Enum.CameraType.Scriptable

local goal1 = {CFrame = Camera2.CFrame}

local Animation1 = TweenService:Create(camera,TweenInformation,goal1)

local goal2 = {CFrame = Camera4.CFrame}

local Animation2 = TweenService:Create(camera,TweenInformation,goal2)


while wait() do

camera.CFrame = Camera1.CFrame

Animation1:Play()

Animation1.Completed:Wait()

camera.CFrame = Camera3.CFrame

Animation2:Play()

Animation2.Completed:Wait()

end

So the problem is that i need for the script to constantly check if the player has this value (which gets deleted once the player presses play), but it only checks once every loop.

Any help would really be appreciated :+1:

1 Like

I think that tweening a black frame across the players screen would be a good idea, and then make the players camera custom or make it switch to a different camera all together. I mentioned the black frame to go across the screen because then the player wont have the potential to see the behind the scenes stuff. Also i dont think that removing the value is necessary because if the player re opens the menu (If thats a feature) then the value would have to be created again, so making it change values would make more sense to me.

1 Like

Okay, so step 1 is to make your loop check if the value exists before starting a new animation…

while wait() and valueFolder:FindFirstChild( 'YourValue' ) do
    camera.CFrame = Camera1.CFrame
    Animation1:Play()
    Animation1.Completed:Wait()
    if not valueFolder:FindFirstChild( 'YourValue' ) then -- Between each animation, check it still exists
        break
    end
    camera.CFrame = Camera3.CFrame
    Animation2:Play()
    Animation2.Completed:Wait()
end

That will prevent starting a new animation if the value is non-existent. Next, you need to cancel an animation in progress when the value is removed.

valueFolder.ChildRemoved:Connect( function( child )
    if child.Name == 'YourValue' then
        Animation1:Cancel()
        Animation2:Cancel()
    end
end )

Replace valueFolder with the path to the folder with this value that you mentioned. Replace ‘YourValue’ with the string name of the value to look for.

1 Like

The problem is that i also want for the player to be able to resume the animations if they decide to go back to the Main Menu, so i’m guessing i need to wrap the while loop in a function which will be called if the player presses the ‘back’ button or resets. The problem with that is that PlayerAdded and CharacterAdded only works on the server side which means i need a separate script which means i can’t call the function at all.

On my own system I usually just have 1 localscript for the client that does everything. But yes the loop should be inside of a “menu camera” function that gets called every time you show the menu. The animation tweens and the ChildRemoved events can remain outside of the function because they don’t need redefining every single time.

If you’re struggling to trigger it again, it’s purely due to where the code is and the access to it. You could use a BindableEvent or something similar to trigger it if you want to keep it in its own script, and again just call a function containing the loop part each time the bindable is triggered.

1 Like

Is there anyway to break out of the loop using a function? I’m trying to make it so all animations immediately stop once the play button is pressed instead of having to wait for the next if statement.

That’s handled by the ChildRemoved event I included. Move the cancel statements into a different function if you want them in a function that you can call whenever you need.

Cancel fires the Completed event, and then the loop is broken based on the conditions you’ve given to it.

1 Like