Problem regarding map generation

Hello developers,

Alright. I’ve been stumped again. I’m trying to create a map generation system-ish that includes placing pipes in a row that are of different types; normally this is pretty simple. Here’s the problem- these pipes keep clipping through each other sometimes, and another functionality that I’ll get to in a bit.

Essentially, I create pipes and then place them on the last pipe’s AttachmentPoints, can be one or multiple. The pipes in question, however, have a tendency to clip through each other, making it impossible to traverse. I have tried adding a solution to this, where I have another pipetype that simply ends in a wall, that I will place whenever there isn’t enough “space” for the selectedPipe. But that doesn’t work either.

Heres my code:
for i = 1, CONFIG_PIPE_SYSTEM_LENGTH, 1 do
	
	print("CREATING PIPE: " .. i)

	if #pipeQueue == 0 then
		
		break
		
	end

	local lastPipe = table.remove(pipeQueue, 1)
	local newOriginPoint = lastPipe.Position
	local selectedPipe = AvailablePipeTypes[math.random(1, #AvailablePipeTypes)]

	local validAttachments = {}
	
	for i, attachment in ipairs(lastPipe:GetChildren()) do
		
		if attachment:IsA("Attachment") then
			
			local newDestinationPoint = attachment.WorldCFrame.LookVector
			local newRaycast = workspace:Raycast(newOriginPoint, newDestinationPoint)
	
			if newRaycast and (newDestinationPoint - newRaycast.Instance.Position).Magnitude < selectedPipe.RequiredSize.Value then
				
				local previousPipe = lastPipe
				
				if previousPipe then
					
					local pipeReplacement = PipeTypes.PipeType3:Clone()
					pipeReplacement.Parent = newPipeline:FindFirstChild("AdditionalPipes")
					pipeReplacement.CFrame = previousPipe.CFrame
					previousPipe:Destroy()
					table.insert(placedPipes, pipeReplacement)
					
				end
				
				continue
				
			end
	
			table.insert(validAttachments, attachment)
		end
		
	end
	
	if #validAttachments > 0 then
		
		for i, attachment in ipairs(validAttachments) do
			
			local clonedPipe = selectedPipe:Clone()
			
			clonedPipe.Parent = newPipeline:FindFirstChild("AdditionalPipes")
			
			clonedPipe.CFrame = attachment.WorldCFrame
	
			table.insert(pipeQueue, clonedPipe)
			table.insert(placedPipes, clonedPipe)
			
		end
		
	else
		
		if lastPipe:IsA("ConnectorPipe") then
			
			for i, attachment in ipairs(lastPipe:GetChildren()) do
				
				if attachment:IsA("Attachment") then
					
					local endPipe = PipeTypes.PipeType3:Clone()
					endPipe.Parent = newPipeline:FindFirstChild("AdditionalPipes")
					endPipe.CFrame = attachment.WorldCFrame
					table.insert(placedPipes, endPipe)
					
				end
				
			end
			
		end
		
	end
	
end

Now I will admit, I’m not a great programmer when it comes to algorithms like this. But I did try the solutions I knew of, and even tried consulting a bit of Ai to see whether it could point me in the right direction, which obviously led to failure.

The two main pipes I am using are the StraightPipe and ConnectorPipe, which I will provide images of right down here:

Images + Extra Info

[Green Orbs are Attachments (you already know this)]
image
[ConnectorPipe] [Has 2 attachment points at either end]
image
[StraightPipe] [Has one attachment point]

If I were to use simply straight pipes, that works fine. But the second the connector pipes come into the equation, everything changes as it’s horizontally different too. I am really struggling with this.

Any help is appreciated,
Ocean

Also, I know that some of the code isn’t optimized. This is supposed to be pseudo code.