Part.CFrame to Part.Motor6D.C0 Problem

I’ve created a simular post like this a couple of days ago which got solved but that was with Part.CFrame, now I am using Motor6D’s.
As far as I know I should get the same result as regular cframing if I just do gun.Head.Motor6D.C0 = gun.Head.CFrame:ToObjectSpace(cf) but that seems to glitch out.
image

--the code used
local r = workspace.CurrentCamera:ScreenPointToRay(mouse.X, mouse.Y, 0)
local cf = CFrame.new(r.Origin,r.Origin+r.Direction)
gun.Head.CFrame = gun.Head.CFrame:ToObjectSpace(cf)

Then I switched to a different approach, I removed the position from cf like this:

local r = workspace.CurrentCamera:ScreenPointToRay(mouse.X, mouse.Y, 0)
local cf = CFrame.new(r.Origin,r.Origin+r.Direction)-r.Origin
gun.Head.Motor6D.C0 = cf

The result wasn’t as glitchy as before, but still incorrect:
image
(The gun is suppost to follow my mouse)

My last sort-of succesfull attempt also changed the Part1 of the Motor6D to the ‘Aim’ part.

local r = workspace.CurrentCamera:ScreenPointToRay(mouse.X, mouse.Y, 0)
local cf = CFrame.new(r.Origin,r.Origin+r.Direction)-r.Origin
gun.Head.Motor6D.Part1 = gun.Aim
gun.Head.Motor6D.C0 = cf

image
This time only the rotation is incorrect.
The Motor6D properties are:

Part0 = character.Head --the default character head
Part1 = gun.Head --this is the main part of the gun, 
--every part in the gun is welded on this part.
Parent = gun.Head

That’s about it, I thank you all for helping me.

Why dont you use mouse:Hit() instead? I think there’s a function in the mouse that returns a Ray’s usual output. Also instead of doing motor6d stuff, what if you used: CFrame.new(pos, lookat) --both are positions

I’ve used mouse.Hit.Position before CFrame.new(character.Head.Position, mouse.Hit.Position) but I never used the ray properties. I assume they will be the exact same tho.
I have a working version for CFrame but I’m using Motor6D because it has smoother replication than CFrame.
Gun gets created on the server with the motor6d, this way roblox automaticly repositions the gun for everyone on the server. Then the owner of the gun does the rotation for the gun. If I used CFrame for this the gun would lack behind the player for everyone except for the gun owner him self.
Thanks for your suggestion tho.

Problem solved.

It’s generally helpful to clarify how you solved it for any future users who encounter the same or a similar issue.

Well the thing it, it got solved in a wierd kind of way.

local lYRot = CFrame.new(r.Origin,r.Origin+r.Direction):ToOrientation()
Motor6D.Part1 = gun.Aim
Motor6D.C0 = CFrame.new(0,-.15,0)  * CFrame.Angles(lYRot,0,0)--this works for me

-0.15 seems to work for pritty much anything I tried (repositioning the Aim part, different resolutions). I don’t know why it is -0.15 I’ll try to figure that out later.