How do I fix this camera switching problem?

I’m not the best at making titles.

  1. What do you want to achieve? I want to achieve a camera switching system (<-- for the first time ever) (NOT a spectate GUI) where it goes around the 4 cameras in workspace.Cameras folder. Basically, if a player presses the next camera or the previous camera button, it would move the player’s camera to the next or previous camera & change the label (for example, camera A, Text: “Camera #1” to Camera B, Text: “Camera #2” & so on). It’s like choosing a plot in bloxburg, it’d switch the cameras to show the plot you’re choosing.

  2. What is the issue? Before I coded stuff for the camera, the label did work, including the current camera label #1-#4 limit. Now that I have the camera code, the 1-4 limit breaks & you can go under 1 or above 4 which breaks the code & outputs errors like “Camera5 not found” or something like that or… “Camera -68 not found”.

  3. What solutions have you tried so far? I have tried changing the CurrentCameraNumber variable into a NumberValue, did not help at all, including the CameraSubject. (I have put the camera type stuff in a different script so that’s why the repeat wait() loop for the camera type isn’t in the script (aka. the script I’m having problems on) that’s handling the changing of cameras)

Video (to explain my idea more): (deleted because I thought streamable would keep your videos if you’re visitor/guest (basically, unregistered user))

Structure:
RobloxStudioBeta_RX23wcA4Xp

Code (handling camera changes) (Excluded hover animations & irrelevant variables):

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local camera = workspace.CurrentCamera
local cameras = workspace:WaitForChild("Cameras")
local camera1 = cameras:WaitForChild("Camera1")
local button1 = script.Parent:WaitForChild("NextButton")
local button2 = script.Parent:WaitForChild("PreviousButton")
local label1 = script.Parent:WaitForChild("CameraLabel")
local currentCameraNumber = 1
local maximumCameraNumber = 4

button1.MouseButton1Click:Connect(function()
	currentCameraNumber = currentCameraNumber + 1
	label1.Text = "Camera #" .. currentCameraNumber
	camera.CFrame = cameras["Camera" .. currentCameraNumber].CFrame
	
	if currentCameraNumber > maximumCameraNumber then
		currentCameraNumber = 1
		label1.Text = "Camera #" .. currentCameraNumber
		camera.CFrame = cameras["Camera" .. currentCameraNumber].CFrame
	end
end)

button2.MouseButton1Click:Connect(function()
	currentCameraNumber = currentCameraNumber - 1
	label1.Text = "Camera #" .. currentCameraNumber
	camera.CFrame = cameras["Camera" .. currentCameraNumber].CFrame

	if currentCameraNumber < 1 then
		currentCameraNumber = maximumCameraNumber
		label1.Text = "Camera #" .. currentCameraNumber
		camera.CFrame = cameras["Camera" .. currentCameraNumber].CFrame
	end
end)

I think it is erroring because your checking if the number is vaild before settingthe textlabel and camera. make sure camera type is scriptable.

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local camera = workspace.CurrentCamera
local cameras = workspace:WaitForChild("Cameras")
local camera1 = cameras:WaitForChild("Camera1")
local button1 = script.Parent:WaitForChild("NextButton")
local button2 = script.Parent:WaitForChild("PreviousButton")
local label1 = script.Parent:WaitForChild("CameraLabel")
local currentCameraNumber = 1
local maximumCameraNumber = 4

button1.MouseButton1Click:Connect(function()
	currentCameraNumber = currentCameraNumber + 1
	
	if currentCameraNumber > maximumCameraNumber then
		currentCameraNumber = 1
	end

    label1.Text = "Camera #" .. currentCameraNumber
	camera.CFrame = cameras["Camera" .. currentCameraNumber].CFrame
end)

button2.MouseButton1Click:Connect(function()
	currentCameraNumber = currentCameraNumber - 1

	if currentCameraNumber < 1 then
		currentCameraNumber = maximumCameraNumber
	end

    label1.Text = "Camera #" .. currentCameraNumber
	camera.CFrame = cameras["Camera" .. currentCameraNumber].CFrame
end)
1 Like

Thanks! :slight_smile:

Plus, yes, I did put it to scriptable in another script.