Good day (or night)! It seems like I’m on here every single day . I’m working on a Backrooms game (not Sonic this time) and I’m trying to make generation for the levels so that they’re not always the same. I’ve created a horrendously unoptimized system and it only generates in 4 directions (like a plus +) and in 2 opposite corners. Here is the code:
local availableChunks = workspace.AvailableChunks
local generatedChunks = workspace.GeneratedChunks
local startRoom = generatedChunks.Start
local prevRoom1 = generatedChunks.Start
local prevRoom2 = generatedChunks.Start
local prevRoom3 = generatedChunks.Start
local prevRoom4 = generatedChunks.Start
local prevRoom5 = generatedChunks.Start
local prevRoom6 = generatedChunks.Start
local prevRoom7 = generatedChunks.Start
local prevRoom8 = generatedChunks.Start
--CONFIG--
local boardSize = 16
local function generate(vector, prevRoom)
local room = availableChunks:GetChildren()[math.random(1,#availableChunks:GetChildren())]:Clone()
local connectorNum = math.random(1,#room.Connect:GetChildren())
local connectorNum2 = math.random(1,#prevRoom.Connect:GetChildren())
room.PrimaryPart = room:FindFirstChild("Connect"):FindFirstChild("C"..connectorNum)
room:PivotTo(prevRoom:FindFirstChild("Connect"):FindFirstChild("C"..connectorNum2).CFrame + Vector3.new(vector))
room.Parent = generatedChunks
prevRoom = room
end
for i=1,boardSize do
local room = availableChunks:GetChildren()[math.random(1,#availableChunks:GetChildren())]:Clone()
local connectorNum = math.random(1,#room.Connect:GetChildren())
local connectorNum2 = math.random(1,#prevRoom1.Connect:GetChildren())
room.PrimaryPart = room:FindFirstChild("Connect"):FindFirstChild("C"..connectorNum)
room:PivotTo(prevRoom1:FindFirstChild("Connect"):FindFirstChild("C"..connectorNum2).CFrame + Vector3.new(16,0,0))
room.Parent = generatedChunks
prevRoom1 = room
end
for i=1,boardSize do
local room = availableChunks:GetChildren()[math.random(1,#availableChunks:GetChildren())]:Clone()
local connectorNum = math.random(1,#room.Connect:GetChildren())
local connectorNum2 = math.random(1,#prevRoom2.Connect:GetChildren())
room.PrimaryPart = room:FindFirstChild("Connect"):FindFirstChild("C"..connectorNum)
room:PivotTo(prevRoom2:FindFirstChild("Connect"):FindFirstChild("C"..connectorNum2).CFrame + Vector3.new(-16,0,0))
room.Parent = generatedChunks
prevRoom2 = room
end
for i=1,boardSize do
local room = availableChunks:GetChildren()[math.random(1,#availableChunks:GetChildren())]:Clone()
local connectorNum = math.random(1,#room.Connect:GetChildren())
local connectorNum2 = math.random(1,#prevRoom3.Connect:GetChildren())
room.PrimaryPart = room:FindFirstChild("Connect"):FindFirstChild("C"..connectorNum)
room:PivotTo(prevRoom3:FindFirstChild("Connect"):FindFirstChild("C"..connectorNum2).CFrame + Vector3.new(0,0,16))
room.Parent = generatedChunks
prevRoom3 = room
end
for i=1,boardSize do
local room = availableChunks:GetChildren()[math.random(1,#availableChunks:GetChildren())]:Clone()
local connectorNum = math.random(1,#room.Connect:GetChildren())
local connectorNum2 = math.random(1,#prevRoom4.Connect:GetChildren())
room.PrimaryPart = room:FindFirstChild("Connect"):FindFirstChild("C"..connectorNum)
room:PivotTo(prevRoom4:FindFirstChild("Connect"):FindFirstChild("C"..connectorNum2).CFrame + Vector3.new(0,0,-16))
room.Parent = generatedChunks
prevRoom4 = room
end
for i=1,boardSize do
local room = availableChunks:GetChildren()[math.random(1,#availableChunks:GetChildren())]:Clone()
local connectorNum = math.random(1,#room.Connect:GetChildren())
local connectorNum2 = math.random(1,#prevRoom5.Connect:GetChildren())
room.PrimaryPart = room:FindFirstChild("Connect"):FindFirstChild("C"..connectorNum)
room:PivotTo(prevRoom5:FindFirstChild("Connect"):FindFirstChild("C"..connectorNum2).CFrame + Vector3.new(16,0,16))
room.Parent = generatedChunks
prevRoom5 = room
end
for i=1,boardSize do
local room = availableChunks:GetChildren()[math.random(1,#availableChunks:GetChildren())]:Clone()
local connectorNum = math.random(1,#room.Connect:GetChildren())
local connectorNum2 = math.random(1,#prevRoom6.Connect:GetChildren())
room.PrimaryPart = room:FindFirstChild("Connect"):FindFirstChild("C"..connectorNum)
room:PivotTo(prevRoom6:FindFirstChild("Connect"):FindFirstChild("C"..connectorNum2).CFrame + Vector3.new(-16,0,-16))
room.Parent = generatedChunks
prevRoom6 = room
end
And here is a picture of the generation and explorer: