# Having Problems With Random Room Generating

I’ve been experimenting around with random room(s) generating, and honestly, it’s my first time messing around with these, so my code might not be the best out there. I quickly put together a code, and I can’t seem to figure out what’s wrong here?

Based on the picture (below), the map is being generated, but the rooms are only being generated one direction, and not the other. Any clue on to why it’s acting like that?

Code:

``````local rooms = game.ReplicatedStorage:WaitForChild("Rooms")

local LastRoom
local CurrentRoom

local loc1 = -100
local loc2 = 0
local loc3 = 0

function createRoom(num)
local WorldChosen = rooms:FindFirstChild('World'..num):GetChildren()
local RoomChosen = WorldChosen[math.random(1,#WorldChosen)]:Clone()
RoomChosen:SetPrimaryPartCFrame(LastRoom.PrimaryPart.CFrame - Vector3.new(loc1,loc2,loc3))
RoomChosen.Parent = workspace.RoomHolder
LastRoom = RoomChosen
if RoomChosen.Name == 'Room1' or RoomChosen.Name == 'Room5' or RoomChosen.Name == 'Room6' then
if loc1 == -100 then
elseif loc3 == -100 then
end
elseif RoomChosen.Name == 'Room2' or RoomChosen.Name == 'Room4' then
if loc1 == -100 then
loc1 = 0
loc2 = 0
loc3 = -100
elseif loc3 == -100 then
loc1 = -100
loc2 = 0
loc3 = 0
end
elseif RoomChosen.Name == 'Room3' then
if loc1 == -100 then
loc1 = 0
loc2 = 0
loc3 = -100
elseif loc3 == -100 then
loc1 = -100
loc2 = 0
loc3 = 0
end
end

RoomChosen.CameraPart.Rotation = Vector3.new(0,-180,0)
end
``````

(Btw, I’m running and looping this function)

Any help is needed! Thanks!

2 Likes

In the picture it seems like it’s being generated both down and left? So I’m not sure what you mean exactly.

Do you mean that the rooms are all in one line instead of branching out? In that case, it’s because of your “add a room to the last room that was added” approach. It can never go back to a room that was added e.g. 3 steps back, so it just becomes one long “snake”. The way to go about this is to keep track of every room “exit” that has no connected room, and then picking any of those exits to add the next room to.

EDIT: E.g. instead of `LastRoom` and `CurrentRoom` you’d have `UnconnectedExits = {}`. Inside each room you can information about where an adjecent room should be for each exit in that room, and add all of them to the `UnconnectedExits`. When choosing where a room goes, it just goes to any exit in that list.

4 Likes

I am extremely sorry about the fact that this reply is not related to the generation but what is the name of the project? Is there anywhere I can follow the development of it? It just looks really neat and a game I’d probably play.

I’m having trouble understanding what you’re trying to say here, can we maybe speak on discord about it, if you don’t mind?

Project I’m working on with a friend of mine, I’ll post more details about it soon!