Fix this randomly generated corridor problem

I made this corridor that is generated from randomly chosen segments and I thought it had no problems but as soon as I make a “segment” that is longer than the others, it completely breaks.

Why do these weird gaps happen, how do I fix this.

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local segments = ReplicatedStorage.Segments:GetChildren()
local starts = ReplicatedStorage.Starts:GetChildren()
local ends = ReplicatedStorage.Ends:GetChildren()

local segment_tables = {}

local max = 10

local cooldown = 0

function ChooseSegment(): Model
	local random = segments[math.random(1, #segments)]
	table.insert(segment_tables, random)
	return random
end

function ChooseStart(): Model
	local random = starts[math.random(1, #starts)]
	local clone = random:Clone()
	clone.Name = "Start"
	clone.Parent = workspace
	return random
end

function ChooseEnd(): Model
	local random = ends[math.random(1, #ends)]
	return random
end

function PositionSegment(lastSegment: Model, segment: Model)
	lastSegment = lastSegment or ChooseStart()
	local sizeOffset = (lastSegment:GetExtentsSize().Z)
	segment:PivotTo(lastSegment:GetPivot() * CFrame.new(0, 0, sizeOffset))
	segment:Clone().Parent = workspace
end



local counter = 0

repeat PositionSegment(segment_tables[math.max(1, #segment_tables)], ChooseSegment()) task.wait(cooldown) counter+=1 until counter >= max

PositionSegment(segment_tables[math.max(1, #segment_tables)], ChooseEnd())
1 Like

I think the pivot of your segment is adjusted right, try edditing it

1 Like

I think you should remove the logic:

local sizeOffset = (lastSegment:GetExtentsSize().Z)
segment:PivotTo(lastSegment:GetPivot() * CFrame.new(0, 0, sizeOffset))

And then add attatchments

(because your logic assumes that every singel segment starts at the same relative origin and that the Z-size alone is enough to place the next piece correctly)

1 Like

Could you elaborate on this?

Summary

char limit

Something like this

function PositionSegment(lastSegment: Model?, segment: Model)
	lastSegment = lastSegment or ChooseStart()
	local lastEnd = lastSegment:FindFirstChild("EndAttach", true)
	local thisStart = segment:FindFirstChild("StartAttach", true)

	if not lastEnd or not thisStart then
		warn("Missing attachment")
		return
	end

	local offset = lastEnd.WorldCFrame * thisStart.CFrame:Inverse()
	local clone = segment:Clone()
	clone:PivotTo(offset)
	clone.Parent = workspace

	table.insert(segment_tables, clone)
end
1 Like

I would rewrite it entirely and use entrances and exits and connect these with eachother

1 Like

Spawns like this now :confused:

Maybe I’m doing something wrong with the attachments

image

Alright, I fixed my original problem but now there’s a new one.

Whenever I try generate the start, although the part(called “begin”) in start is rotated the right way and in the right place, this happens when I try to spawn it:

As you can see, it’s overlapping and it is the wrong way round

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local segments = ReplicatedStorage.Segments:GetChildren()
local starts = ReplicatedStorage.Starts:GetChildren()
local ends = ReplicatedStorage.Ends:GetChildren()

local segment_tables = {}

local max = 10

local cooldown = 0

function ChooseSegment(): Model
	local random = segments[math.random(1, #segments)]
	table.insert(segment_tables, random)
	return random
end

function ChooseStart(): Model
	local random = starts[math.random(1, #starts)]
	local clone = random:Clone()
	clone.Name = "Start"
	clone.Parent = workspace
	return random
end

function ChooseEnd(): Model
	local random = ends[math.random(1, #ends)]
	return random
end

local part = workspace.Begin

function PositionSegment(segment: Model)
	local clone = segment:Clone()
	clone:PivotTo(part.CFrame)
	clone.Parent = workspace
	
	part = clone.End
end



local counter = 0

repeat PositionSegment(ChooseSegment()) task.wait(cooldown) counter+=1 until counter >= max

ChooseStart():PivotTo(segment_tables[1].Start.CFrame)

How do I fix this.

Can someone help me please with this

Could you send the whole file?

1 Like

Place1.rbxl (141.3 KB)
Here’s the place file