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)
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.
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?