Rotate character after camera rotation reaches a threshhold

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

  1. What do you want to achieve? Keep it simple and clear!
    I want to rotate the character after the rotation of the camera reaches a certain threshold

  2. What is the issue? Include screenshots / videos if possible!
    I have a script that sets the camera in custom shift lock turning the humanoid.AutoRotate to false and UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter. I also have another script that rotates the head based on mouse position and i thought i would rotate the character based on that but it seems like a very bad way to do it as it doesnt always work sometimes it gets stuck rotating and sometimes it doesnt even rotate and im trying to search for another way to turn the character after reaching a threshhold

Here is my current script:

local RunService = game:GetService"RunService"
local UserInputService = game:GetService"UserInputService"
local Player = game.Players.LocalPlayer
local PlayerMouse = Player:GetMouse()
local RunService = game:GetService("RunService")
local Camera = workspace.CurrentCamera

local Character = Player.Character or Player.CharacterAdded:Wait()
-- R6 components
local Head = Character:WaitForChild("Head")
local Torso = Character:WaitForChild("Torso")
local Humanoid = Character:WaitForChild("Humanoid")
--Humanoid.AutoRotate = false


local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")

local Torso = Character:WaitForChild("Torso")
local Neck = Torso:WaitForChild("Neck")

local Waist = HumanoidRootPart:WaitForChild("RootJoint")

local RHip = Torso:WaitForChild("Right Hip")
local LHip = Torso:WaitForChild("Left Hip")

local LHipOriginC0 = LHip.C0
local RHipOriginC0 = RHip.C0

local NeckOriginC0 = Neck.C0
local WaistOriginC0 = Waist.C0

Neck.MaxVelocity = 1/3
local mouse = game:GetService("Players").LocalPlayer:GetMouse()

--mouse blacklist

--Camera.CameraType = Enum.CameraType.Scriptable
local p1 = Instance.new("Part",Character)
p1.CanCollide = false
local motor6d = Instance.new("Motor6D",Character.Torso)
motor6d.Part0 = Torso
motor6d.Part1 = p1
motor6d.C0 = Neck.C0
left = false
right = false
mouse.TargetFilter = workspace
lastang = 0

lp = Vector3.new(0,0,0)

RunService.Heartbeat:Connect(function()
	
	--UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
	--Humanoid.CameraOffset = Vector3.new(2,2,1)
	
	local CameraCFrame = HumanoidRootPart.CFrame * CFrame.new(-10,3,-3)

	if Character:FindFirstChild("Torso") and Character:FindFirstChild("Head") then
		local TorsoLookVector = Torso.CFrame.lookVector
		local HeadPosition = Head.CFrame.p

		if Neck and Waist then
			if Camera.CameraSubject:IsDescendantOf(Character) or Camera.CameraSubject:IsDescendantOf(Player) then
				local Point1 = mouse.Hit.p

				local Distance = (Head.CFrame.p - Point1).magnitude
				local Difference = Head.CFrame.Y - Point1.Y


				local goalNeckCFrame = CFrame.Angles(-(math.atan(Difference / Distance) * 1), (((HeadPosition - Point1).Unit):Cross(TorsoLookVector)).Y * 1.5, 0)
		
					Neck.C0 = Neck.C0:lerp(goalNeckCFrame*NeckOriginC0, 0.5 / 2).Rotation + NeckOriginC0.Position
				local Point = mouse.Hit.p
				local HeadPosition = Vector3.new(Head.Position.X, 0, Head.Position.Z)
				local PointFlattened = Vector3.new(Point.X, 0, Point.Z)
				local TorsoLookVector = Vector3.new(Torso.CFrame.LookVector.X, 0, Torso.CFrame.LookVector.Z).Unit

				-- Calculate the direction and angle
				local Direction = (PointFlattened - HeadPosition).Unit
				local angle = math.atan2(Direction.Z, Direction.X) - math.atan2(TorsoLookVector.Z, TorsoLookVector.X)
				angle = -angle -- Invert angle if needed

				-- Clamp the angle to prevent large differences
				local maxDeltaAngle = math.rad(5) -- Maximum allowable angle change per frame in radians
				local clampedAngle = math.clamp(angle, -maxDeltaAngle, maxDeltaAngle)

				-- Apply rotation to the head
				local goalNeckCFrame = CFrame.Angles(0, clampedAngle, 0)
				motor6d.C0 = motor6d.C0:Lerp(goalNeckCFrame * NeckOriginC0, 0.25) 
				
				
				
				cframe = Neck.C0:lerp(goalNeckCFrame*NeckOriginC0, 0.5 / 2).Rotation + NeckOriginC0.Position
				local calc = Neck.Part0.CFrame:toObjectSpace(cframe)
				local x,y,z = calc:ToOrientation()
			
				--print(z)
				local difference = math.abs(angle - lastang)

				-- Check if the difference exceeds the threshold
				
				
				right = false
				left = false
				if angle <-1 then	
					right = true
				--print("right")
				elseif angle > 1 then
					--print("left")
				
					left = true
				end
				--local xAxisWaistRotation = -(math.atan(Difference / Distance) * 0.5)
			
				local tws = game:GetService("TweenService")
				local a = 0
			print( angle)
			if right then
					--HumanoidRootPart.CFrame *= CFrame.Angles(0,- 0.05, 0)
			elseif left then
					--HumanoidRootPart.CFrame *= CFrame.Angles(0, 0.05, 0)
			end
				
                
				


				local currentLegCounterCFrame = Waist.C0*WaistOriginC0:Inverse()

				local legsCounterCFrame = currentLegCounterCFrame:Inverse()


				--Camera.CFrame = HumanoidRootPart.CFrame *goalNeckCFrame * CFrame.new(0,3,10)
				--Camera.CFrame = CFrame.new(Camera.CFrame.Position, Point)
               lastang = angle
			end
		end
	end	
end)

any help is appreciated !!