Motor6D snapping instead of moving smoothly

Im trying to make this tank turret smoothly rotate, but when I move over a certain angle, it freezes and snaps into place.

Video:

Code:

local RotateM6D = Turret["Left/Right"].PivotBase.PivotWeld
		local TiltM6D = Turret["Up/Down"].PivotBase.PivotWeld

		local RotateDir = Vector3.new(screenRayDir.X,screenRayDir.Y,screenRayDir.Z)
		local RLworldRotationCFrame = CFrame.new(RotateM6D.Part0.Position, RotateDir)

		local rotateGoalCF = M6DController.cframeObjectRotation(RotateM6D, RLworldRotationCFrame)
		local relativeCF = RotateM6D.Part0.CFrame:ToObjectSpace(CFrame.lookAt(RotateM6D.Part0.Position, screenRayDir, RotateM6D.Part0.CFrame.UpVector))
		local rx, ry, rz = relativeCF:ToOrientation()

		local clampedRY = math.clamp(math.deg(ry), Settings.XLimit[1], Settings.XLimit[2])
		local rotateC0 = CFrame.new(rotateGoalCF.Position) * CFrame.Angles(0,math.rad(clampedRY),0)
		
		local oldX, oldY, oldZ = RotateM6D.C0:ToOrientation() -- old orientation
		local newX, newY, newZ = rotateC0:ToOrientation() -- new orientation
		local displacement = newY - oldY -- distance
		
		local tweenTime = displacement * (Settings.TurretRotationSpeedX / 100)
		
		local rotateTweenInfo = TweenInfo.new(tweenTime, Enum.EasingStyle.Linear)
		
		local rotateTween = TweenServ:Create(RotateM6D, rotateTweenInfo, {C0 = rotateC0})
		rotateTween:Play()

Print out the angles and see if anything weird is happening at that border area.

To me it looks like the code is struggling when 0 becomes 360 and vice versa.

Yeah I believe this is exactly the issue, when the angle goes from -180 to 0 and vice versa the problem happens. Im Not sure how to fix it.

I actually made some code for this, but I don’t feel comfortable giving it away. In any case, you just need to make the angle fit in with the current angle.

You need to make it so that if the angle is -180 and the new angle is 1, the new angle becomes -181.
(hint compare the distances if you subtracted -180, added 0, or added 180 and think about what it would be for something like -360)