Two rooms spawning in the same place(Tell me if there are any other bugs its my first time making room gen)

When I use this script(In ServerScriptService, in some cases, two rooms will spawn in the same place


Code:

local rooms = workspace.Rooms
local startingRoom = workspace.StartingRoom
local maderoom = 0

local function spawnNextHallway(prevRoom)
	local hallways = rooms:GetChildren()
	local rand = Random.new():NextInteger(1, 6)
	local nextHallway = hallways[rand]
	if prevRoom.Name == "HallwayLeft"  then
		nextHallway = rooms.StraightHallway
	end
	if prevRoom.Name == "HallwayRight"  then
		nextHallway = rooms.StraightHallway
	end
	if prevRoom.Name == "StraightHallway"  then
		nextHallway = rooms.HallwayLeft
	end
	local hallway : Model= nextHallway:Clone()
	local Doors = prevRoom:WaitForChild("Doors"):GetChildren()
	hallway.Parent = workspace
	local door = Doors[Random.new():NextInteger(1,  #Doors)]
	hallway:PivotTo(door.CFrame)
	door:Destroy()
	
	for i, v in Doors do
		if v.Name =="Door1" then
			v.Transparency = 1
		else
			v.Transparency = 0
			v.BrickColor = BrickColor.new("Teal")
			v.Material = Enum.Material.Concrete
			v.MaterialVariant = 'Concrete 2'
		end
		
	end
	
	return hallway
end

local prevroom = spawnNextHallway(startingRoom)

while maderoom < 100 do
	prevroom = spawnNextHallway(prevroom)
	maderoom += 1
end

(Tell me if there are any more bugs

2 Likes

Its the fact that your hallways can just generate “HALLWAYLEFT”,“HALLWAYLEFT” ,“HALLWAYLEFT” ,“HALLWAYLEFT” , I would recommend using a bag system, where its a table of hallways you plan to have, and then determine how many lefts/rights you had.

You don’t have any room hit detection system, meaning if rooms overlap they will generate inside of eachother

Maybe try this code.

local startingRoom = workspace.StartingRoom
local madeRoom = 0
local randomGenerator = Random.new()

local function spawnNextHallway(prevRoom)
    local hallways = rooms:GetChildren()
    local rand = randomGenerator:NextInteger(1, math.min(6, #hallways))
    local nextHallway = hallways[rand]
    
    if prevRoom.Name == "HallwayLeft" or prevRoom.Name == "HallwayRight" then
        nextHallway = rooms.StraightHallway
    elseif prevRoom.Name == "StraightHallway" then
        nextHallway = rooms.HallwayLeft
    end
    
    local hallway: Model = nextHallway:Clone()
    local doors = prevRoom:WaitForChild("Doors"):GetChildren()
    hallway.Parent = workspace
    
    local door = doors[randomGenerator:NextInteger(1, #doors)]
    hallway:PivotTo(door.CFrame)
    door:Destroy()
    
    for i, v in ipairs(doors) do
        if v.Name == "Door1" then
            v.Transparency = 1
        else
            v.Transparency = 0
            v.BrickColor = BrickColor.new("Teal")
            v.Material = Enum.Material.Concrete
            v.MaterialVariant = Enum.MaterialVariant.Concrete2 -- Use Enum value
        end
    end
    
    return hallway
end

local prevRoom = spawnNextHallway(startingRoom)

while madeRoom < 100 do
    prevRoom = spawnNextHallway(prevRoom)
    madeRoom += 1
end


Ahh yes, the new mechanic, Random.new()

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