Hey everyone!
I am trying to create a procedural room generation system and I am having trouble with the procedural element to it. I need my system to randomly choose a room and connect it to the previous rooms that have been generated.
What needs to happen is that the script needs to randomly select a main room and clone it, which I have done.
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local mainFolder = ReplicatedStorage:WaitForChild("mapGeneration"):WaitForChild("main")
local mapFolder = Workspace:FindFirstChild("map")
if not mapFolder then
mapFolder = Instance.new("Folder")
mapFolder.Name = "map"
mapFolder.Parent = Workspace
end
local function selectRandomModel(folder)
local models = folder:GetChildren()
if #models == 0 then
warn("No models found in the 'main' folder")
return nil
end
local randomIndex = math.random(1, #models)
return models[randomIndex]
end
local function setupMainRoom()
local modelToClone = selectRandomModel(mainFolder)
if modelToClone then
local clonedModel = modelToClone:Clone()
clonedModel.Parent = mapFolder
if clonedModel.PrimaryPart then
clonedModel:SetPrimaryPartCFrame(CFrame.new(0, 0.5, 0))
else
warn("Selected model does not have a PrimaryPart")
end
end
end
setupMainRoom()
However, my trouble is when I need clone a random room and align the door “connectors” to eachother by moving the primary parts (the room floors). It needs to expand from the main room that I have cloned.
As you can see in this screenshot, each door has a connector which I planned to use to connect one room to another. Also then comes the question of rooms colliding. I beleive I must use a grid system which is why my room sizes are a multiple of 8 which I have done.
I also thought that it would be a good idea if I made the pivot of the rooms in a top corner to allow for easy rotation but I am not entirely sure.
Any tips/support will be MUCH appreaciated on how I can acheive this system.