You can use CFrame:ToOrientation() then clamp the angles in orientation form which I have done in my turret controller to reconstruct the CFrame with the newer clamped angles.

Also are you angles in orientation terms or eulerangles XYz terms the order of operations the angles are applied is a big deal and a commong problem on #help-and-feedback:scripting-support

Unfortunately I need everything to be relative to the attachments rotation, for example, AngleY will Yaw a part towards the White Sphere when applied like this:

If I only use the Green Sphere’s position, it will no longer be relative. The problem I’m having is when I apply each angle by them selves, it works as intended:

You would want to generate a CFrame relative to the attachment then
You could probably just take the lookat CFrame and multiply that by the inverse of the world CFrame of the attachment

So

local x,y,z = (attachment.WorldCFrame:Inverse()*CFrame.new(attachment.WorldPosition,lookPosition)):ToEulerAnglesXYZ()
thing.CFrame = attachment.WorldCFrame*CFrame.Angles(x,y,z)

You are using an angles solution? I dislike angles as they have weird behavior like orientation goes from 180 to -180 which causes those bugs.

Also what you are doing looks like inverse kinematics. Check it out, even if it’s not related it’s got cool CFrame tricks there which might help.

I have already implemented ballsocket constraints:

Ballsocket constraint related part

local centerAxis = axisAttachment.WorldAxis
local currentCenterAxis = jointAttachment.WorldAxis
local angleDifference = VectorUtil.AngleBetween(currentCenterAxis, centerAxis)
local constraintUpperAngle = math.rad(jointConstraintInfo.UpperAngle) or math.rad(45)
--out of bounds constrain it to world axis of the socket
if angleDifference > constraintUpperAngle then
local axis = currentCenterAxis:Cross(centerAxis)
local angleDifference = angleDifference - constraintUpperAngle
local newCenterAxisWithinBounds = rotateVectorAround(currentCenterAxis, angleDifference, axis)
self.rotateJointFromTo(motor6d, currentCenterAxis, newCenterAxisWithinBounds, part0CF.RightVector)
end