local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local InsertService = game:GetService("InsertService")

local ModulesFodler = ReplicatedStorage:FindFirstChild("Modules")
local SegmentsFolder = ReplicatedStorage:FindFirstChild("Segments")

local seg_whitelist = { "StartPiece", "EndPiece" };

local AssetLinks = require(ModulesFodler:FindFirstChild("AssetLinks"))

local seg_total = 6 -- Total segments to generate per hallway
local seg_length = 40 -- Studs to manually set an offset
local seg_priority = {}; -- Priority table to handle duplicates
local generation_counter = 0 -- Tracks segments

local function isNumeric(name)
	return tonumber(name) ~= nil
end

local function GenerateSegments()
	local seg_numeric = {};
	for _, segment in pairs(SegmentsFolder:GetChildren()) do
		if isNumeric(segment.Name) and not table.find(seg_whitelist, segment.Name) then
			table.insert(seg_numeric, tonumber(segment.Name))
		end
	end
	
	return seg_numeric
end

local function ValidateSegments(seg_numeric)
	local seg_valid = {};
	for _, num in pairs(seg_numeric) do
		local segment = SegmentsFolder:FindFirstChild(tostring(num))
		if segment or AssetLinks[num] then
			table.insert(seg_valid, num)
		end
	end
	
	return seg_valid
end

local function GetNextSegment(seg_valid)
	if #seg_priority > 0 then
		local priority = table.remove(seg_priority, 1)
		return priority
	end
	
	local segment
	repeat
		segment = seg_valid[math.random(1, #seg_valid)]
	until not table.find(seg_priority, segment) or #seg_priority >= 3
	
	table.insert(seg_priority, segment)
	if #seg_priority > 3 then
		table.remove(seg_priority, 1)
	end
	
	return segment
end

local function GetSegmentInstance(num)
	local segment = SegmentsFolder:FindFirstChild(tostring(num))
	if not segment and AssetLinks[num] then
		segment = InsertService:LoadAsset(AssetLinks[num]):GetChildren()[1]
	end
	
	return segment
end

local function GenerateHallway()
	local segments = GenerateSegments()
	if #segments == 0 then
		error("No valid segments found")
	end
	
	local seg_valid = ValidateSegments(segments)
	if #seg_valid == 0 then
		error("No valid exist in-game or as rbxassetid links")
	end
	
	local hallway = Instance.new("Model")
	hallway.Name = "GeneratedHallway"
	hallway.Parent = workspace
	
	local pos_current = Vector3.new(0, 0, 0)
	
	local pc_start = SegmentsFolder:FindFirstChild("StartPiece")
	if pc_start then
		local cl_start = pc_start:Clone()
		cl_start:SetPrimaryPartCFrame(CFrame.new(pos_current))
		cl_start.Parent = hallway
		pos_current = pos_current + Vector3.new(0, 0, seg_length)
	end
	
	for i = 1, seg_total do
		local seg_next = GetNextSegment(seg_valid)
		local seg_instance = GetSegmentInstance(seg_next)
		
		if not seg_instance then
			if #seg_priority > 0 then
				seg_next = table.remove(seg_priority, 1)
				seg_instance = GetSegmentInstance(seg_next)
			else
				error("Failed to generate next segment. Reload the game")
			end
		end
		
		local clone = seg_instance:Clone()
		clone:SetPrimaryPartCFrame(CFrame.new(pos_current))
		clone.Parent = hallway
		pos_current = pos_current + Vector3.new(0, 0, seg_length)
	end
	
	local pc_end = SegmentsFolder:FindFirstChild("EndPiece")
	if pc_end then
		local cl_end = pc_end:Clone()
		cl_end:SetPrimaryPartCFrame(CFrame.new(pos_current))
		cl_end.Parent = hallway
	end
end

GenerateHallway()

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: