Help with Camera Manipulation

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve?

Trying to learn how to create a third person camera that allows a person to look around themselves without need to press the right mouse button but locks to the front view of a person when the right mouse button is pressed.

  1. What is the issue?

I just got back to it and I simply can’t wrap my head around this, I was able to make the camera move around my character, but I barely understood that.

The script is local and it is supposed to take the player’s current camera position and based on changes the player makes, the camera will adjust to the player’s location however my issue is with using the mouse to make changes to the camera.

  1. What solutions have you tried so far?

I tried using the mouse delta to use the changes in mouse movement to move the camera, but I have no idea how to script this correctly.

local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local UserInputService = game:GetService("UserInputService")
local ContextActionService = game:GetService("ContextActionService")

local player = Players.LocalPlayer

local camera = workspace.CurrentCamera
local mouse = player:GetMouse()
local Switch = false

--Values

local cameraOrientation = {-25, 0, 0}

local cameraAngleX = 0
local cameraAngleY = 0

local function scriptedCamera()
	if workspace.CurrentCamera and player.Character then
		local humanoidRootPart = player.Character:WaitForChild("HumanoidRootPart")
		if humanoidRootPart then
			
			player.Character:WaitForChild("Humanoid").AutoRotate = false
			
			camera.CameraType = Enum.CameraType.Scriptable
			camera.CameraSubject = humanoidRootPart
			
			local rootPosition = humanoidRootPart.Position + Vector3.new(0, 2, 0)
			cameraPosition = Vector3.new(2,8,3)
			camera.CFrame = CFrame.lookAt(cameraPosition, rootPosition)
			
			print("worked")
		end
	end
end

local function updateCamera()
	if workspace.CurrentCamera and player.Character then
		local humanoidRootPart = player.Character:WaitForChild("HumanoidRootPart")
		if humanoidRootPart then

			local rootPosition = humanoidRootPart.Position + Vector3.new(0, 2, 0)
			local cameraPosition = Vector3.new(rootPosition.X + 3, rootPosition.Y + 2, rootPosition.Z + 3)
			camera.CFrame = CFrame.lookAt(cameraPosition, Vector3.new(rootPosition.X + 2, rootPosition.Y + 1, rootPosition.Z))
			print("worked2")
		end
			local function playerInput(ActionName, inputState, inputObject)
				if inputState == Enum.UserInputState.Change then
			
					UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
					local Delta = UserInputService:GetMouseDelta()
					print(Delta)

					cameraAngleX = cameraAngleX - inputObject.Delta.X
					cameraPosition = cameraAngleX
			
			end
		end
		if Switch == false then
			Switch = true
			ContextActionService:BindAction("PlayerInput", playerInput, false, Enum.UserInputType.MouseMovement, Enum.UserInputType.Touch)
		end
	end
end

player.CharacterAdded:Connect(function()
	wait(1)
	scriptedCamera()
	print("Played")
end)

RunService:BindToRenderStep("Camera", Enum.RenderPriority.Camera.Value + 1, updateCamera)