HELP with wall building system

Hi. I have made a wall building system like bloxburg. Mine however snaps on to the nearest 90 degrees. I need a bit of help because it works fine however there is a gap in the corners. I think this is something to do with the distance between the two poles. Here is my code:

		local ray = Ray.new(PositionA, (PositionA - PositionB).unit * Mag)
		
		local part, position,normal = workspace:FindPartOnRayWithIgnoreList(ray, returnIgnoreList())

		local distance = (PositionA - position).magnitude
		--beam.Size = Vector3.new(ReplicatedStorage:FindFirstChild(Item).Size.Z, 10, distance)

		local End = PositionB
		local Start = PositionA
		local Distance = (End - Start).Magnitude
		
		Mouse.TargetFilter = beam
		
		local units = {
			[2] = Vector3.new(1, 0, 0);
			[3] = Vector3.new(1, 0, 0);

			[4] = Vector3.new(0, 0, -1);
			[5] = Vector3.new(0, 0, -1);

			[6] = Vector3.new(-1, 0, 0);
			[7] = Vector3.new(-1, 0, 0);

			[8] = Vector3.new(0, 0, 1);
			[1] = Vector3.new(0, 0, 1);
		}

		local function get_Beam_CFrame(origin_Pos, end_Pos)
			local delta_Pos = end_Pos - origin_Pos
			local radians = math.atan2(delta_Pos.X, delta_Pos.Z) + math.pi
			local degrees = math.deg(radians)
			local unit_Index = math.ceil(degrees/45)

			local distance = delta_Pos.Magnitude
			local look_Pos = origin_Pos + units[unit_Index]

			local beam_CFrame = CFrame.new(origin_Pos, look_Pos) * CFrame.new(0, 0, distance / 2) + Vector3.new(0,5,0)
			return beam_CFrame
		end
		
		
		beam.CFrame = get_Beam_CFrame(Start,End)
		beam.Size = Vector3.new(1,10, Distance)
		BeamCFrame = beam.CFrame
		BeamSize = beam.Size
		TempBuild = beam

Many thanks!

please show the whole code for this :wink:

it’s deprecated and should be replaced with workspace:Raycast(PositionA, (PositionA - PositionB).unit * Mag, rayParams), and use RaycastParams.new() like this:

local rayParams = RaycastParams.new()
rayParams.FilterDescendantsInstances = {} -- items to filter, put parts
rayParams.RaycastFilterType = Enum.RaycastFilterType.Exclude

I have replaced the deprecated ray function. Thank you for that! The problem I have is I only want to be able to move my mouse left or right. The post currently follows my mouse and the beam is calculated by the 2 posts. I can move my post diagonally and I only want it to be left and right as my system snaps to 90 degrees. Is there any way I can go about doing this?