I’m afraid the solution is more complicated than getting the angles between the two parts. You’ll need to mess with CFrames.
Here’s two handy contructors: CFrameFromTopRight and CFrameFromTopFront. You’ll probably find yourself using these a lot:
-- Constructs a CFrame such that its position is p,
-- its upVector is top, and its lookVector is front
function CFrameFromTopFront(p, top, front, right)
if not right then
right = front:Cross(top).unit
end
local top = top.unit
local front = front.unit
return CFrame.new(
p.X, p.Y, p.Z,
right.X, top.X, -front.X,
right.Y, top.Y, -front.Y,
right.Z, top.Z, -front.Z
)
end
-- Constructs a CFrame such that its position is p,
-- its upVector is top, and its rightVector is right
function CFrameFromTopRight(p, top, right, front)
if not front then
front = top:Cross(right).unit
end
local top = top.unit
local right = right.unit
return CFrame.new(
p.X, p.Y, p.Z,
right.X, top.X, -front.X,
right.Y, top.Y, -front.Y,
right.Z, top.Z, -front.Z
)
end
If the three parts are named redPart, greenPart, and bluePart, then the CFrame of the bluePart will be this:
-- distance between red and green
local dist = (greenPart.Position-redPart.Position).magnitude
-- direction from red to green; bluePart will face this direction
local dir = (greenPart.Position-redPart.Position).unit
-- construct a CFrame whose up-direction is red's up direction and faces dir
local bluePartCFrame = CFrameFromTopFront(
redPart.Position + dir*dist/2, -- bluePart's position is in-between green and red
dir:Cross(redPart.CFrame.rightVector.unit).unit,
dir
)
But you’re asking for a weld offset (and judging by the script in the op, a value for C0). The script above gives the WorldSpace CFrame (i.e. the actual CFrame for bluePart in the world), not a weld offset. For that, you’ll need to convert the CFrame above from world space to object space relative to redPart:
Assuming your weld’s name is weld, this is how you’d do it:
weld.C0 = redPart.CFrame:toObjectSpace(bluePartCFrame)
EDIT: I just realized that this solution was wrong. I’ve edited it. It might be right this time.