How I can generade path (roads) as well connect them, without getting empty spaces between them?

So far my road generation script does this:


Is there a way I could connect them?

How I can connect them?

local LastRoad = workspace.Part

function placeRoad(dir)
	local Part = Instance.new("Part")
	if dir == "forward" then
		Part.Size = Vector3.new(4, 0.2, 2)
		Part.Position = LastRoad.Position + Vector3.new(Part.Size.X, 0, 0)
	end
	if dir == "left" then
		Part.Size = Vector3.new(2, 0.2, 4)
		Part.Position = LastRoad.Position + Vector3.new(Part.Size.X + 1, 0, -1)
	end
	if dir == "right" then
		Part.Size = Vector3.new(2, 0.2, 4)
		Part.Position = LastRoad.Position + Vector3.new(Part.Size.X + 1, 0, 1)
	end
	Part.Anchored = true
	Part.CanCollide = true
	Part.Parent = workspace
	LastRoad = Part
end

while wait(0.5) do
	local c = math.random(1, 3)
	if c == 1 then
		placeRoad("forward")
	end
	if c == 2 then
		placeRoad("left")
	end
	if c == 3 then
		placeRoad("right")
	end
end

--placeRoad("right")

I didn’t start the generation part at all, I just made completely random placing script, since I was testing

Bumping this topic/post, since there is no answers.

you could make a generator where each part will see the possible outcomes of the path while keeping sure of collision and correct road types to prevent the path intercepting each other, or the roads becoming detached like in your photo.

if you need a specified path, you could use PathfindingService

Any examples of code? I don’t really know where to start of with connecting the roads accordingly to the path, without creating some strange shapes.

After using PathFinding Server I have got this:


After some time experimenting

1 Like

1 - Get the magnitude from the current point to the next point
local Distance = (NextPoint.Position-CurrentPoint.Position).magnitude
2 - Get the halfway position between those 2, you can use
local Pos = CurrentPoint.Position:Lerp(NextPoint.Position, 0.5)
3 - Create the road part

local Part = Instance.new("Part")
Part.Size = Vector3.new(1,1,Distance)
Part.CFrame = CFrame.new(Pos, NextPoint.Position) -- In the middle of the 2 points and looking at the nextPoint.
Part.Parent = workspace.Roads -- Better storing your parts in a folder

So simply you’re going to do this in a loop. getting the CurrentPoint and NextPoint until there is no NextPoint :slight_smile:

1 Like

Also quick question, how I would able to prevent those gaps?


I kind of fix it, but there still gaps ;/

1 Like

Maybe try to resize the part slightly bigger than the distance?
if you want a smoother and better result. you’ll need to use some triangles and math and of course more parts to generate some kind of mesh.
But i can’t really help you with such thing since i never expermented with it enough. I bet there is better people to help you with generating such thing. Good luck!

1 Like

Yeah I did try to resize parts a bit, Thank you for your help anyways!

2 Likes