CFrame alignment/placement issue aligning part to anonther part

I got a problem a placing a part (variable name: “struct”) next to another part (“hit”).

Here are 3 parts, the blue part (hit) is the part i am aligning the grey part (struct) to:

For this i use a slightly modified version of the code i use to align boxes to wedges/triangles, Boxes are 10,10,10 in size, the grey part are 1,10,10:

local length = (Struct.Size.Z / 2 + Struct.Size.X / 2) -- or 5.5
newPos = (Hit.CFrame + (Hit.CFrame.RightVector * length)) * CFrame.new(0, 0, (10 - Hit.Size.Z) / 2)

I use the PivotTo and PivotOffset to rotate the parts, but thats not relevant here, as its not a problem.

What i do here is to get the RightVector then multiply it by the lenght, i.e. how far i want to be placed away from the centre of the Hit.CFrame position.

The problem is, i want the part to align to the red part, and whatever i do- modify the “length” modify the CFrame by say moving -5 studs in x direction, there always is a small misalignment of something like 0.01 - 0.05 studs.


Here in the picture above i have moved the CFrame -.5 in the z direction, this does not work. I have also tried getting the RightVector of the blue part (hit) and multiply it by .5, that does not either work.

Geting myabe part about think is

-- Create a new part
local newPart = Instance.new("Part")

-- Set properties for the part (you can customize these)
newPart.Size = Vector3.new(10, 5, 2)
newPart.Position = Vector3.new(0, 10, 0)
newPart.BrickColor = BrickColor.new("Bright red")

-- Parent the part to the workspace
newPart.Parent = game.Workspace