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())
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)
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
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)