Increase/decrease failing

My goal:
Button ‘Next’ should transition to +1 camera (cam1 to cam2) for example
Button ‘Previous’ should transition to -1 camera (cam2 to cam1) for example

Issue:
The system itself works but the increase/decrease does not, I get thrown at random cameras
(cam1 to cam3 to cam6 and so on)

And the output is showing no errors.

local camera = workspace.CurrentCamera
local prompt = workspace.MainPart.ProximityPrompt
local breakLoop = false

local camTable = {
	cam1 = workspace.Plot.Camera1,
	cam2 = workspace.Plot.Camera2,
	cam3 = workspace.Plot.Camera3,
	cam4 = workspace.Plot.Camera4,
	cam5 = workspace.Plot.Camera5,
	cam6 = workspace.Plot.Camera6
}

local camKeys = {"cam1","cam2","cam3","cam4","cam5","cam6"}
local currentCamIndex = 1

prompt.Triggered:Connect(function(player)
	breakLoop = false
	while breakLoop == false do
		
		local gui = player.PlayerGui:FindFirstChild("CameraGui")
		gui.Enabled = true
		camera.CameraType = Enum.CameraType.Scriptable
		camera.CFrame = camTable[camKeys[currentCamIndex]].CFrame
		
		gui.Frame.Next.MouseButton1Click:Connect(function()
			currentCamIndex += 1
			if currentCamIndex > #camKeys then
				currentCamIndex = 1
			end
			camera.CFrame = camTable[camKeys[currentCamIndex]].CFrame
		end)

		gui.Frame.Previous.MouseButton1Click:Connect(function()
			currentCamIndex -= 1
			if currentCamIndex < 1 then
				currentCamIndex = #camKeys
			end
			camera.CFrame = camTable[camKeys[currentCamIndex]].CFrame
		end)
		
		gui.Frame.Select.MouseButton1Click:Connect(function()
			breakLoop = true
			gui.Enabled = false
			camera.CameraType = Enum.CameraType.Custom
			camera.CFrame = player.Character.Head.CFrame
		end)		
		wait()
	end
end)

Why don’t you make the keys in camTable a number? Or no table at all?

camTable[1] --> Camera1
workspace.Plot["Camera"..currentCamIndex] --> Camera1

Also I see your connecting events in a while loop, which I strongly recommend you change. I don’t think you understand how events work.

replace the camtable with this

local camTable = {
	[1] = workspace.Plot.Camera1,
	[2] = workspace.Plot.Camera2,
	[3] = workspace.Plot.Camera3,
	[4] = workspace.Plot.Camera4,
	[5] = workspace.Plot.Camera5,
	[6] = workspace.Plot.Camera6
}

delete the camKeys then use
camTable[currentCamIndex]

also dont use while loops in this case it’s unnecessary

you are making signal connections in a while loop. Every iteration of the while loop adds a new connection, each connection will increase the currentCamIndex so it will feel random. If you add a print statement in any of these functions you will see that they are called hundreds or thousands of times and will eventually lag your game. To fix this you need to remove the while loop and maybe use :Once instead of :Connect I am not sure what the prompt triggering is doing but you will have connections remaining even after you delete the while loop.

Try this? I fixed a few things to make it work the way I think you want it to.

local camera = workspace.CurrentCamera
local prompt = workspace.MainPart.ProximityPrompt
local gui = player.PlayerGui:WaitForChild("CameraGui")
local cameraindex = 1
local cameras = 6

local function updatecam()
    camera.CameraType = Enum.CameraType.Scriptable
	camera.CFrame = workspace.Plot["Camera"..cameraindex].CFrame
end

prompt.Triggered:Connect(function(player)
    cameraindex = 1
    updatecam()
    gui.Enabled = true		
end)

--\\ Controls

gui.Frame.Next.MouseButton1Click:Connect(function()
    cameraindex += 1
    if cameraindex > cameras then
        cameraindex = 1
    end
    updatecam()
end)

gui.Frame.Previous.MouseButton1Click:Connect(function()
    cameraindex -= 1
    if cameraindex < 1 then
        cameraindex = cameras
    end
    updatecam()
end)

gui.Frame.Select.MouseButton1Click:Connect(function()
    gui.Enabled = false
    camera.CameraType = Enum.CameraType.Custom
end)
2 Likes

It works as in it fixes the increase/decrease issue I had, but I forgot to mention that I had the ‘while’ loop because my cameras are rotating and constantly moving. Do you have a suggestion?

set the cameraType to scriptable

It is set to scriptable 30char

did u anchor the camera parts?

or u meant your cameras are moving around as you wanted to?

The camera part is unanchored, it has an attachment part that is anchored, in the middle and the camera rotates around it

(it works perfectly fine, the parts themselves)

1 Like

Figured it out. Added a while loop inside a function to change the camera constantly and call it when prompt is triggered. Thanks for the help

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.