Calculating y-offset semi-works for placement system

This is what happens when i place stuff

image

My current script

Position is from raycasting the mouse position

local CF, Size = ClientStructure:GetBoundingBox()

local NewAnglesCFrame = CFrame.Angles(0, math.rad(YOrientation), 0)
local NewCFrame = CFrame.new(Position.X, Position.Y + Size.Y/2, Position.Z)
ClientStructure:PivotTo(NewCFrame * NewAnglesCFrame)

I want the fence to be on the ground, not floating

You might also want to cast a ray from the endpoints of the part to the ground to see what they hit and then align it to a suitable height.