Trouble Aligning Parts In a Building System

I made a post about this sometime last year, but after many months it was never answered. I’m trying to make a building system, but I have this problem with adding a rotation feature to the building:

Here is my code, a local script parented to the tool (note: the player.Inventory.Wood is an IntValue storing how much wood the player has, since building requires wood.):

local player = game.Players.LocalPlayer

local uis = game:GetService("UserInputService")

while (not player.Character) or (not player:FindFirstChild("Inventory")) do
	wait()
end

local hover = game.ReplicatedStorage.WoodCube:Clone()
hover.Parent = game.Workspace
hover.BrickColor = BrickColor.new("Electric blue")
hover.Transparency = 0.5
hover.CanCollide = false
hover.Transparency = 1

local mouse = player:GetMouse()

script.Parent.Equipped:Connect(function()
	
	hover.Transparency = 0.5
	mouse.TargetFilter = hover

	while script.Parent.Parent:FindFirstChild("Humanoid") do
		
		if (not player.Inventory:FindFirstChild("Wood")) or not ((hover.Position - script.Parent.Parent.HumanoidRootPart.Position).magnitude <= 30) then

			hover.BrickColor = BrickColor.new("Bright red")

		else
			
			hover.BrickColor = BrickColor.new("Electric blue")
			
		end
		
		if mouse.TargetSurface.Name == "Top" then
			hover.Position = mouse.Hit.Position + Vector3.new(0, (hover.Size.Y / 2), 0)
		elseif mouse.TargetSurface.Name == "Bottom" then
			hover.Position = mouse.Hit.Position + Vector3.new(0, -(hover.Size.Y / 2), 0)
		elseif mouse.TargetSurface.Name == "Left" then
			hover.Position = mouse.Hit.Position + Vector3.new(-(hover.Size.X / 2), 0, 0)
		elseif mouse.TargetSurface.Name == "Right" then
			hover.Position = mouse.Hit.Position + Vector3.new((hover.Size.X / 2), 0, 0)
		elseif mouse.TargetSurface.Name == "Back" then
			hover.Position = mouse.Hit.Position + Vector3.new(0, 0, (hover.Size.Z / 2))
		elseif mouse.TargetSurface.Name == "Front" then
			hover.Position = mouse.Hit.Position + Vector3.new(0, 0, -(hover.Size.Z / 2))
		else
			hover.Position = mouse.Hit.Position + Vector3.new(0, (hover.Size.Y / 2), 0)
		end

		wait()

	end

	hover.Transparency = 1
	
end)

mouse.Button1Down:Connect(function()
	
	if (script.Parent.Parent:FindFirstChild("Humanoid")) and (player.Inventory:FindFirstChild("Wood")) and (not (mouse.Target == nil)) then
		
		if (hover.Position - script.Parent.Parent.HumanoidRootPart.Position).magnitude <= 30 then
			
			game.ReplicatedStorage.PlaceObject:FireServer(hover.CFrame)
			
		end
		
	end
	
end)

uis.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.R then
		hover.Rotation += Vector3.new(0, 90, 0)
	end
end)

These are the images of it working, before rotating the part (White dot = mouse cursor, red dot = center of part to be placed, pink line = offset applied):

Then these are the same positions, but now with the part rotated 90 degrees on the y-axis:

The issue is that no matter the rotation of the part being placed, the offset from the part (or terrain) is always the same as if it weren’t rotated. I need a solution to figure out the distance needed to offset the part being placed from the existing one enough that they are touching, but not overlapping, as seen in the first three images.

And I do apologize for pretty much reposting the same topic, but I can’t figure this out and the game can’t be continued in really any way at all until I have a solution. I also know the code is not the best, but I can always refine it later. Any help would be appreciated.