Here's an example to make it understandable: How to make a cannon look at a target while being attached to a car?
The barrel part is welded to the cylinder using Weld, and the Weld is used to set CFrames of the whole barrel (make it rotate freely) and that's what I'm aiming for. The only problem is the CFrames math which I do not know how.
Roblox has an example just for that on the wiki if you look at CFrame.fromMatrix
function lookAt(target, eye) local forwardVector = (eye - target).Unit local upVector = Vector3.new(0, 1, 0) local rightVector = forwardVector:Cross(upVector) local upVector2 = rightVector:Cross(forwardVector) return CFrame.fromMatrix(eye, rightVector, upVector2) end
Make sure target is the target objects position and the eye is the the turret's LookVector, also make sure this is all done on the server and not the client.
So for example I would call that function like this:
ObjectA.CFrame = lookAt(ObjectB.Position, ObjectA.CFrame.LookVector)
Make sure to substitute ObjectA and ObjectB with your actual objects.