Help with camera script

Hello everyone, I am currently making a doors like game and I tried making the torso bend with the player camera and, I used a fake head and put it as the cameraSubject. But when swimming it becomes very buggy and glitchy.

Here is my script,

task.spawn(function()
	RunService.PostSimulation:Connect(function()
		if character and humanoidRootPart and weld.Enabled and character.Locker.Value == nil then
			local X, Y, Z = currentCamera.CFrame:ToOrientation()
			local x, y, z = humanoidRootPart.CFrame:ToOrientation()
			humanoidRootPart.CFrame = CFrame.new(humanoidRootPart.Position) * CFrame.fromEulerAnglesYXZ(x, Y, z)
			local waist = character:FindFirstChild("UpperTorso") and character.UpperTorso:FindFirstChild("Waist")
			if humanoid:GetState() ~= Enum.HumanoidStateType.Swimming then
				if waist then
					waist.C1 = CFrame.new(0, -0.8 * character:GetScale(), 0) * CFrame.Angles(X, 0, 0):Inverse()
				end
			else
				if waist then
					waist.C1 = CFrame.new(0, -0.8 * character:GetScale(), 0)
				end
			end
			local angleX = math.deg(X)
			local currentTime = tick()
			if math.abs(lastX - angleX) > 0.1 and (currentTime - lastFireTime) >= debounceThreshold then
				remote:FireServer(X, 0, 0)
				lastX = angleX
				lastFireTime = currentTime					
			end				
		end
		specialMesh.Offset = weld.C0:Inverse().Position
	end)
end)