Help with random terrain generation

Hi guys so I am working on this random terrain generation for my upcoming game on roblox the issue here is I want a way to add borders (barrier parts) as seen in the image here. Currently when i tried this I used it alongside partcache everything here is actually done by that module. You can see the gray parts are not really forming a wall correctly. If anyone got any ideas on how to archieve something like this please let me know.

heres a code snippet of how I did it:

function IslandManager:UpdateInvisibleWalls(player)
if not wallCache then return end

local islandData = playerIslandData[player.UserId]
if not islandData then return end

local wallsContainer = islandData.IslandFolder:FindFirstChild("Walls") or Instance.new("Folder", islandData.IslandFolder)
wallsContainer.Name = "Walls"

for _, wall in ipairs(islandData.AllWalls) do
	wallCache:ReturnPart(wall)
end
islandData.AllWalls = {}

local currentSize = (islandData.IslandFolder:FindFirstChild("CurrentSize") or {}).Value or Config.NEW_PLAYER_ISLAND_SIZE
local origin = self:GetIslandOrigin(player)

local horizontalEdges = {}
local verticalEdges = {}
local halfBlock = Config.BLOCK_SIZE / 2

-- 1. Find all raw shoreline edges within the visible area
for key, tile in pairs(islandData.TileMap) do
	if (tile.PrimaryPart.Position - origin).Magnitude <= currentSize then
		local parts = string.split(key, ",")
		local r, c = tonumber(parts[1]), tonumber(parts[2])
		local tilePos = tile.PrimaryPart.Position
		
		-- Check Top/Bottom (Horizontal Edges)
		if not islandData.TileMap[(r-1) .. "," .. c] then table.insert(horizontalEdges, {p1=tilePos+Vector3.new(-halfBlock,0,-halfBlock), p2=tilePos+Vector3.new(halfBlock,0,-halfBlock)}) end
		if not islandData.TileMap[(r+1) .. "," .. c] then table.insert(horizontalEdges, {p1=tilePos+Vector3.new(-halfBlock,0,halfBlock), p2=tilePos+Vector3.new(halfBlock,0,halfBlock)}) end
		-- Check Left/Right (Vertical Edges)
		if not islandData.TileMap[r .. "," .. (c-1)] then table.insert(verticalEdges, {p1=tilePos+Vector3.new(-halfBlock,0,-halfBlock), p2=tilePos+Vector3.new(-halfBlock,0,halfBlock)}) end
		if not islandData.TileMap[r .. "," .. (c+1)] then table.insert(verticalEdges, {p1=tilePos+Vector3.new(halfBlock,0,-halfBlock), p2=tilePos+Vector3.new(halfBlock,0,halfBlock)}) end
	end
end

-- 2. Merge co-linear edges into longer segments
local mergedHorizontal = mergeEdges(horizontalEdges)
local mergedVertical = mergeEdges(verticalEdges)

local allMergedEdges = {}
for _, edge in ipairs(mergedHorizontal) do table.insert(allMergedEdges, edge) end
for _, edge in ipairs(mergedVertical) do table.insert(allMergedEdges, edge) end

-- 3. Create a single wall Part for each merged segment
for _, edge in ipairs(allMergedEdges) do
	local wall = wallCache:GetPart()
	local wallLength = (edge.p1 - edge.p2).Magnitude
	local wallPosition = (edge.p1 + edge.p2) / 2 + Vector3.new(0, Config.BLOCK_SIZE/2, 0) -- Place on top of tile
	local wallCFrame = CFrame.new(wallPosition, edge.p1 + Vector3.new(0, Config.BLOCK_SIZE/2, 0))
	
	wall.Size = Vector3.new(0.1, Config.INVISIBLE_WALL.Height, wallLength)
	wall.CFrame = wallCFrame
	wall.Parent = wallsContainer
	table.insert(islandData.AllWalls, wall)
end

end

I don’t think we’re going to have much chance at debugging this. Have you tried color coding the wall directions and seeing if it matches what you expect? I would also make them slightly transparent so you can see if multiple walls are being stacked on the same spot.