Camera turning at the default angle when equipping a gun

  1. What do you want to achieve? Hello, i am currently working on my third person gun system.

  2. What is the issue? The camera turns back to the default angle when equipping the gun, as you can see on this video
    2023-02-18 18-47-29

This is the script i use to make the camera over the shoulder

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

local camera = workspace.CurrentCamera
local player = Players.LocalPlayer

print(camera.CFrame.Position)

local function charAdded(character)
	local humanoid = character:WaitForChild("Humanoid")
	local rootPart = character:WaitForChild("HumanoidRootPart")

	local cameraAngleX = 0
	local cameraAngleY = 0

	local function playerInput(actionName, inputState, inputObject)
		if inputState == Enum.UserInputState.Change then
			cameraAngleX -= inputObject.Delta.X
			cameraAngleY = math.clamp(cameraAngleY - inputObject.Delta.Y * 0.4, -75, 75)
		end
	end
	ContextActionService:BindAction("PlayerInput", playerInput, false, Enum.UserInputType.MouseMovement, Enum.UserInputType.Touch)

	RunService:BindToRenderStep("CameraUpdate", Enum.RenderPriority.Camera.Value, function()
		local startCFrame = CFrame.new(rootPart.CFrame.Position) * CFrame.Angles(0, math.rad(cameraAngleX), 0) * CFrame.Angles(math.rad(cameraAngleY), 0, 0)
		local cameraCFrame = startCFrame:PointToWorldSpace(script.CameraOffset.Value)
		local cameraFocus = startCFrame:PointToWorldSpace(Vector3.new(script.CameraOffset.Value.X, script.CameraOffset.Value.Y, -1000000))

		camera.CFrame = CFrame.lookAt(cameraCFrame, cameraFocus)

		local lookingCFrame = CFrame.lookAt(rootPart.Position, camera.CFrame:PointToWorldSpace(Vector3.new(0, 0, -1000000)))

		rootPart.CFrame = CFrame.fromMatrix(rootPart.Position, lookingCFrame.XVector, rootPart.CFrame.YVector)
	end)
end

player.CharacterAdded:Connect(function(character)
	charAdded(character)
end)

(I enable it / disable it when i equip / unequip my gun)

  1. What solutions have you tried so far? I tried to fix it myself and looked on forums.