Camera System Help

I have a camera system then whenever you click a GUI button, the users local camera will change, but it doesn’t change the cameras. (Im using a numerical value)

while wait() do

	local cam = workspace.CurrentCamera
	local camPart
	local mouse = game:GetService("Players").LocalPlayer:GetMouse()

	repeat
		cam.CameraType = Enum.CameraType.Scriptable	
		wait()
	until cam.CameraType == Enum.CameraType.Scriptable	

if script.Parent.Value == 1 then
		camPart = workspace.LoadoutCameras.Camera1
	elseif script.Parent.Value == 2 then
		camPart = workspace.LoadoutCameras.Camera2
	elseif script.Parent.Value == 3 then
		camPart = workspace.LoadoutCameras.Camera3
	elseif script.Parent.Value == 4 then
		camPart = workspace.LoadoutCameras.Camera4
	elseif script.Parent.Value == 5 then
		camPart = workspace.LoadoutCameras.Camera5
   end

	local maxTilt = 1
	game:GetService("RunService").RenderStepped:Connect(function()
		cam.CFrame = camPart.CFrame * CFrame.Angles(
			math.rad((((mouse.Y - mouse.ViewSizeY / 2) / mouse.ViewSizeY)) * -maxTilt),
			math.rad((((mouse.X - mouse.ViewSizeX / 2) / mouse.ViewSizeX)) * -maxTilt),

			0
		)
		
	
	end)
	end

GUI scripts

script.Parent.MouseButton1Click:Connect(function()
	if workspace.LoadoutCameras.CameraSelected.Value < 1 then
		workspace.LoadoutCameras.CameraSelected.Value -= 1
	else
		workspace.LoadoutCameras.CameraSelected.Value = 5
	end

end)
script.Parent.MouseButton1Click:Connect(function()
	if workspace.LoadoutCameras.CameraSelected.Value < 5 then
		workspace.LoadoutCameras.CameraSelected.Value += 1
	else
		workspace.LoadoutCameras.CameraSelected.Value = 1
	end
	
end)

Hey Aiden, I really love this idea of a camera system. I see the problem with your script! I will attempt to help you clean and fix the script.
Something I would like to comment on in your code is that you looped a renderstepped which would cause a crazy amount of stress/lag on the client as well as causing a Memory leak. Try not to put multiple loops inside of eachother.
I have labeled the code so that you can know how the code works and what I changed.

repeat task.wait() until workspace:FindFirstChild('LoadoutCameras') -- Wait For Game To Load

-- > Declarations < -- 

local cam = workspace.CurrentCamera -- Our Camera
local camPart -- Localizeing so we can call this outside of loops
local mouse = game:GetService("Players").LocalPlayer:GetMouse() -- Our plr mouse
local maxTilt = 1 -- Maxtilting of camera
local Workspace = game:GetService('Workspace')
local LoadoutCameras = Workspace:WaitForChild('LoadoutCameras');

-- // GUI Pressing 

script.Parent.Back.MouseButton1Click:Connect(function() -- Back Arrow Clicking (Previous Camera)
	if LoadoutCameras.CameraSelected.Value > 1 then -- If the CameraSelected value is greater than 1
		LoadoutCameras.CameraSelected.Value -= 1 -- Change camera one less
	else
		LoadoutCameras.CameraSelected.Value = 5 -- if camera is pressed below 1 then set it to 5
	end
end)

script.Parent.Next.MouseButton1Click:Connect(function() -- Forward arrow clicking (Next Camera)
	if LoadoutCameras.CameraSelected.Value < 5 then -- If the CameraSelected value is less than 5
		LoadoutCameras.CameraSelected.Value += 1 -- change camera to next cam
	else
		LoadoutCameras.CameraSelected.Value = 1 -- if camera is pressed above 5 then set it to 1
	end
end)

-- > Loops() < -- 
task.spawn(function() -- Wrap the wait inside these walls
	while wait() do -- loop

		repeat -- repeat setting our camera type to Scriptable until it is Scriptable
			cam.CameraType = Enum.CameraType.Scriptable	
			wait()
		until cam.CameraType == Enum.CameraType.Scriptable	
		local CamValue = LoadoutCameras.CameraSelected.Value -- CurrentCamera Value, 
		if CamValue >= 1 and CamValue <= 5 then -- make sure the camera is within correct values
			camPart = LoadoutCameras['Camera' .. tostring(CamValue)] -- Set the camPart to the new camera value
		end
	end
end)

game:GetService("RunService").RenderStepped:Connect(function() -- RenderSteped Loop
	if cam and camPart then -- if we can find the cam and the camPart
		cam.CFrame = camPart.CFrame * CFrame.Angles( -- Set the Camera CFrame to the camPart CFrame multiplied by the CFrame angles of given math.rad
			math.rad((((mouse.Y - mouse.ViewSizeY / 2) / mouse.ViewSizeY)) * -maxTilt), -- Yaw up/down
			math.rad((((mouse.X - mouse.ViewSizeX / 2) / mouse.ViewSizeX)) * -maxTilt), -- Yaw Left/Right

			0 -- Roll pitch which is set to 0 because we are not rolling (duh)
		)
	end
end)

Please let me know if you need anything else! Happy Scripting.
~ Ihaveash0rtname

1 Like

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