Procedural Generation Failing

Im currently working on a horror game that has procedural generation. However whenever I play the game 2 rooms are always stuck in one! It is two models not a model combining into one to be clear. I need it so that this never happens!

Screen Shot 2022-10-23 at 9.58.09 pm|690x436
(the bookshelf and carpet is a seperate room from the tv and couch)

There was nothing I could find online and I tried editing random spots of the code where it seems it would work but nothing worked!

Please help me if you can!

local rooms = 50

local roomStorage = game.ServerStorage:WaitForChild("DungeonsServerStorage"):WaitForChild("ROOMS")
local doorStorage = game.ServerStorage.DungeonsServerStorage:WaitForChild("DOOR TYPES")


local roomsContainer = Instance.new("Folder")
roomsContainer.Name = "Dungeon"
roomsContainer.Parent = workspace


function createRoom(lastRoom)

	local currentRooms = #roomsContainer:GetChildren()

	if currentRooms < rooms then
		local numRooms = math.random(1, math.clamp(rooms - currentRooms, 1, 4))

		local walls = lastRoom.Walls:GetChildren()

		for i = #walls, 2, -1 do
			local j = Random.new():NextInteger(1, i)
			walls[i], walls[j] = walls[j], walls[i]
		end

		local createdRooms = {}

		for i = 1, numRooms do

			if walls[i] and walls[i]:IsA("Part")  then

				local wallCF = walls[i].CFrame

				local doorsList = doorStorage:GetChildren()
				local newWall = doorsList[math.random(1, #doorsList)]:Clone()
				newWall.CFrame = wallCF
				newWall.Parent = walls[i].Parent

				local wallName = walls[i].Name
				walls[i]:Destroy()


				local roomCFrame = lastRoom.Floor.CFrame + (wallCF.LookVector * lastRoom.Floor.Size.X)
				local isRoom = false

				for x, room in pairs(roomsContainer:GetChildren()) do

					if room.Floor.Position == roomCFrame.Position then
						isRoom = true

						if wallName == "Back Wall" and room.Walls:FindFirstChild("Front Wall") then
							room.Walls["Front Wall"]:Destroy()
						elseif wallName == "Front Wall" and room.Walls:FindFirstChild("Back Wall") then
							room.Walls["Back Wall"]:Destroy()
						elseif wallName == "Left Wall" and room.Walls:FindFirstChild("Right Wall") then
							room.Walls["Right Wall"]:Destroy()
						elseif wallName == "Right Wall" and room.Walls:FindFirstChild("Left Wall") then
							room.Walls["Left Wall"]:Destroy()
						end
					end
				end

				if not isRoom then

					local roomsList = roomStorage:GetChildren()
					local newRoom = roomsList[math.random(1, #roomsList)]:Clone()
					newRoom.PrimaryPart = newRoom.Floor

					newRoom:SetPrimaryPartCFrame(roomCFrame)

					if wallName == "Back Wall" and newRoom.Walls:FindFirstChild("Front Wall") then
						newRoom.Walls["Front Wall"]:Destroy()
					elseif wallName == "Front Wall" and newRoom.Walls:FindFirstChild("Back Wall") then
						newRoom.Walls["Back Wall"]:Destroy()
					elseif wallName == "Left Wall" and newRoom.Walls:FindFirstChild("Right Wall") then
						newRoom.Walls["Right Wall"]:Destroy()
					elseif wallName == "Right Wall" and newRoom.Walls:FindFirstChild("Left Wall") then
						newRoom.Walls["Left Wall"]:Destroy()
					end

					newRoom.Parent = roomsContainer

					table.insert(createdRooms, newRoom)
				end
			end
		end

		for i, createdRoom in pairs(createdRooms) do
			createRoom(createdRoom)
		end
	end
end


local startRoom = roomStorage:WaitForChild("Empty Room"):Clone()
startRoom.PrimaryPart = startRoom.Floor
startRoom:SetPrimaryPartCFrame(CFrame.new(Vector3.new(0, startRoom.Floor.Size.Y/2, 0)))

startRoom.Parent = roomsContainer

createRoom(startRoom)
1 Like

i abandoned the auto generation thing just build the map

Create the x amount of rooms you want and group them as models, then put them all into ServerStorage, give them a primarypart that indicates the exit, and you have a realistic lock-on system so that they don’t bunch together.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.