Problems with rotating part using AssemblyAngularVelocity

  1. What do you want to achieve? Keep it simple and clear!
    So, I am trying to adjust the AssemblyAngularVelocity of a part so that it matches another part’s orientation.
  2. What is the issue? Include screenshots / videos if possible!
    Everything is going fine, until an axis of the part’s orientation goes over 90 or under -90.
    2024-01-27 19-28-59.mp4 - Google Drive
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I tried looking, but I can’t seem to find anything.

Script:

local function euler(cfram)
	local x,y,z = cfram:ToEulerAnglesXYZ()
	local e = Vector3.new(x,y,z)
	return e
end
while task.wait() do
	local e = (euler(workspace.red.CFrame) - euler(script.Parent.CFrame)).Unit
	local x = e.X*2
	local y = e.Y*2
	local z = e.Z*2
	if x < 0.5 and x > -0.5 then
		x = 0
	end
	if y < 0.5 and y > -0.5 then
		y = 0
	end
	if z < 0.5 and z > -0.5 then
		z = 0
	end
	--x = math.round(x)
	--y = math.round(y)
	--z = math.round(z)
	e = Vector3.new(x,y,z)
	if (e.X == e.X) and (e.Y == e.Y) and (e.Z == e.Z) then
		print(e)
		script.Parent.AssemblyAngularVelocity = e
		script.Parent.AssemblyLinearVelocity = (workspace.red.Position - script.Parent.Position)*10
	else
		script.Parent.AssemblyAngularVelocity = Vector3.new(0,0,0)
	end
end

Can someone please respond, this is an issue that has been bothering me for days. I would really appreciate it if any of you can give me a fix.