Need help on camera movement

I know there’s a lot of threads regarding this topic, however none of them fits my scenario.

I’m currently working on a third person over shoulder camera but I ran into some problems, it works normally without anything:

However, when the camera is being setted without moving the mouse, with a code like this:

while wait() do
cam.CFrame = cam.CFrame * CFrame.Angles(0.01,0,0)

The camera angles keep reseting when moving the mouse, on InputChanged:

I can’t really figured out where went wrong? Or how’d I fix this?

local rot
local rotCFrame
local cameraFocus

local mouseDeltaX = 0
local mouseDeltaY = 0

userInputService.InputChanged:Connect(function(input, isProcessed)
	if input.UserInputType == Enum.UserInputType.MouseMovement and lockcam == true then
		local x,y,z = cameraFocus:ToEulerAnglesXYZ()
		cam.CFrame = cam.CFrame * CFrame.Angles(x,y,z)

		userInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
		cam.CFrame = cam.CFrame *,0,0)
		local delta = userInputService:GetMouseDelta()
		mouseDeltaX = mouseDeltaX-delta.x * 0.45
		mouseDeltaY = math.clamp(mouseDeltaY-delta.y * 0.45 , -80, 80)
		local rot =, mouseDeltaY) * math.rad(UserSetting.MouseSensitivity)
		rotCFrame = CFrame.Angles(0, rot.X, 0) * CFrame.Angles(rot.Y, 0, 0) 
		cameraFocus = cam.CFrame:ToObjectSpace(rotCFrame)

while wait() do
	cam.CFrame = cam.CFrame * CFrame.Angles(0.01,0,0)