How could I make my talk show cameras better?

I’m trying to revamp my old talk show game and want to use the old code from the cameras but the way its set up you have to press the key again in order to switch cameras or back out of the cameras

Code for the cameras

local UIS = game:GetService("UserInputService")
local Camera1Debounce = false

UIS.InputBegan:Connect(function(input, gameProcessed)
	if input.KeyCode == Enum.KeyCode.Q and gameProcessed == false then
       if Camera1Debounce == false then
           Camera1Debounce = true
           game.Players.LocalPlayer.Backpack.Cameras.Disabled = false
       else
           Camera1Debounce = false
           workspace.CurrentCamera.CameraType = Enum.CameraType.Custom
           game.Players.LocalPlayer.Backpack.Cameras.Disabled = true --code that changes to camera one
       end
    --add more keys later on using the same thing above but change the KeyCode in correspondence to the camera.
	end
end)

image

Then the 3 actual cameras are in workspace

2 Likes

Not the greatest system since you need to make a new local script for each new camera, you can simply just use 1 local script

local CameraParts = { -- This can be replaced with a folder:GetChildren()
    workspace.Camera1,
    workspace.Camera2,
    workspace.Camera3
}

local CurrentCamera = 1

UIS.InputBegan:Connect(function(input, gameProcessed)
	if input.KeyCode == Enum.KeyCode.Q and gameProcessed == false then
            Camera.CameraType = "Scriptable"
            Camera.CFrame = CameraParts[CurrentCamera].CFrame
            CurrentCamera = CurrentCamera + 1
	end
end)

^ example, if you want it where you can go to the previous camera then simply subtract currentcamera by 1 instead of adding

1 Like

It’s possible that each camera works off its own keypress (even though they all manipulate the camera in the same way unless the script provided was just a template), nevertheless a single local script as you suggested would still suffice. Also I’m sure you likely know this but CurrentCamera = CurrentCamera + 1 can be replaced with CurrentCamera += 1. The following is directed towards the original poster only (as I assume you just copied and pasted the code). It would be more efficient to check the processed condition first (in the current implementation the keypress is checked if it is Q and then the processed check is performed).

Would I put that in the starterpack scripts?

Ok the main problem I see is that you are not setting your CameraType to Scriptable. That means that you can’t mess with any of the camera’s properties.

Second to make this more efficient I would only use one key to go through a table of all the cameras like Thedagz has mentioned. When the camera is to go back to the player you can use a separate key, or use your CurrentCamera index used to change your camera.

The code used is built off of the structure Thedagz used.

An example with a different key:

--variables

UIS.InputBegan:Connect(function(input, gameProcessed)
    if gameProcessed == false then
        if input.KeyCode == Enum.KeyCode.Q then
            if CurrentCamera > #CameraParts then
                CurrentCamera = 1
            end
            Camera.CameraType = Enum.CameraType.Scriptable
            Camera.CFrame = CameraParts[CurrentCamera].CFrame
            CurrentCamera = CurrentCamera + 1
        elseif input.KeyCode == Enum.KeyCode.P then
            Camera.CameraType = Enum.CameraType.Custom
        end
    end
end)

Of course you need to remember to reset the CurrentCamera index when it goes over 3 because then you will run into an error since there are no more than 3 cameras in your table.

Example using the CurrentCamera index:

--variables

UIS.InputBegan:Connect(function(input, gameProcessed)
    if input.KeyCode == Enum.KeyCode.Q and gameProcessed == false then
        if CurrentCamera > #CameraParts then
            CurrentCamera = 1
            Camera.CameraType = Enum.CameraType.Custom
        else
            Camera.CameraType = Enum.CameraType.Scriptable
            Camera.CFrame = CameraParts[CurrentCamera].CFrame
            CurrentCamera = CurrentCamera + 1
        end
    end
end)

Again there are multiple ways of doing this, but these are some efficient ones. It is always better to practice scripting through efficient writing and learning rather than shorter bad practices that seem to work. Doing it this way can even sometimes help solve problems that may occur in the future!