Dungeon generator is not working as intended

Hey, I have a dungeon generator that’s supposed to pick one of (currently) 2 levels to generate, each level is different in colour and props and stuff.
image

For some reason, the generator is mixing and mashing both of these levels, resulting in both levels being merged when they’re not supposed to be


It should look something like this:

My current code looks like this (not all of it):
local rooms = math.random(25,100)

local roomStorage = game.ServerStorage:WaitForChild("DungeonStuff"):WaitForChild("LEVELS")
local doorStorage = game.ServerStorage.DungeonStuff:WaitForChild("DOOR TYPES")

local levels = {}

for _, levelFolder in pairs(roomStorage:GetChildren()) do
	table.insert(levels, levelFolder)
end

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

function createRoom(lastRoom)
	local levelSelected = levels[math.random(1,#levels)]
	local currentRooms = #roomsContainer:GetChildren()
	
	print("Selected Level: " ..levelSelected.Name)

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

		local createdRooms = {}

		-- Missing code

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


local startRoom = levels[math.random(1, #levels)]:WaitForChild("EmptyRoom"):Clone()
startRoom.PrimaryPart = startRoom.Floor
startRoom:SetPrimaryPartCFrame(CFrame.new(Vector3.new(-12.5, 0.5, -5.25))) -- Starting room's position

startRoom.Parent = roomsContainer

createRoom(startRoom)

Any idea what could be causing the issue? I’d appreciate some help, thank you in advance

3 Likes

Can I have more of the explorer like the door types or all of the stuff that is being used?

1 Like

Door types are not the problem right now, those aren’t causing any issues, it’s just the rooms themselves.

1 Like

Okay, is there a certain part of the code that decides what the chosen room is?

1 Like

This picks the level, and by extension the rooms inside of the level folder

2 Likes

Does this function get called for every room?

1 Like

This probably doesn’t tackle any issues but I used to create random room generators before.

Here’s a sample code of what I did:

local x = 10 --amount of rooms in length
local y = 10 --amount of rooms in width
local roomsize = 10 --replace this with the room size, assuming it's a square
local CF = CFrame.new(0,0,0)
local Orig = CF

local function Create(Dim)
if Dim == 1 then
CF += CFrame.new(roomsize,0,0) --New room spawnpoint set
--Generate room and set the primarypartcframe to CF
else
CF = CFrame.new(Orig.X,0,CF.Y+roomsize) --New room spawnpoint set
--Generate room and set the primarypartcframe to CF
end

for i = 1, x do
Create(2) --The parameter doesn't really matter
for h = 1, y do
task.wait(0.1) --Lag prevention yay
Create(1) --Don't change this unless you change some stuff in the function as well
end
end

Hopefully I minimized the amount of typos and errors.
But this shouldn’t make rooms overlap and generates in a grid.

2 Likes

There is a point in the script where this is ran:

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

This isn’t the issue though, disabling it only generates about 3 rooms and enabling it generates a normal amount of rooms. Regardless of whether this is enabled or not, it still mixes up the rooms

1 Like

So this only prints once?

print("Selected Level: " ..levelSelected.Name)

1 Like

you’re picking a random level everytime you create a room which results in having randomized rooms when you create them, put it outside of the function and it’ll work

5 Likes

I tried this before and it wasn’t working, but I’ll try it again and fix up the code a bit accordingly

Edit:
So it seems to sort of work now, the only issue now is the spawn room sometimes being from the wrong level. Level 2 would spawn and the spawn room would be from level 1, it doesn’t always happen though

2 Likes

If this isn’t fixed yet I got a fully functional and customizable infinite room generator you could pay me for; customizable stats are room size, position, starting/set rooms, chances of rooms that can be edited, whatever

2 Likes

I’ve never made a maze generator before, but I believe the only way to ensure that no rooms collide is by using non euclidean geometry. That means, hide rooms that are not touching the one you are in, so that paths can overlap with no issues.

This will need some extra logic to make sure the player doesn’t see the sky, and that the game knows what path they’re on. It may also need instructions on which walls on the room are open to spawn another room!

3 Likes

You could just make the rooms with each side having a boolean for “open” or “closed”, that is, if there are openings in the walls you can go through, and line them up accordingly; I did this but with doors, not open or closed walls

2 Likes

why would I pay for a resource that was offered to me in scripting support when I can ask for help in fixing some minor issues in my mostly completed code

1 Like

The issue here is that every time you are creating an attached segment, you are generating a new random number for level, meaning that it will not be the same for each segment. You need to have a constant value outside of the function to fix this. (so it doesn’t change each time you add a segment)

3 Likes

idk just saying if you want more rooms than 2, plus easy modifications and no bugs

1 Like

This gives me a good idea on a fix, thank you!!

1 Like

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