Doors Room spawn system keeps returning NIL

I’m trying to make a room generation system like Doors, but for some reason, in the loop i try to generate a room using the room generation function, which is being fired from a separate Module script, which has no problems.

For some reason, after generating a few rooms, it wont fully generate by the variable of roomBuffer, and will keep saying nil. Does anyone have any idea why it keep doing so?

Server Script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PossibleRooms = ReplicatedStorage.Rooms
local room = require(script.Room)

local previousRoom = workspace.Lobby

local roomBuffer = 5
local maxRooms = 25
local generatedRooms = {previousRoom}

for i = 1,roomBuffer do
	previousRoom = room.Generate(previousRoom)
	generatedRooms[i]=previousRoom
end

This is the entire room generation script:

local TS = game:GetService("TweenService")

local room = {}
room.info = require(script.RoomInfo)
room.door = require(script.Door)
room.furniture = require(script.Furniture)
room.lastTurnDirection = nil
room.Random = Random.new()
room.mathRandom = math.random()

local GameAssets = game.ReplicatedStorage.GameAssets


function room.FlickerLight(light)
	local originalBrightness = light.Brightness
	local info = TweenInfo.new(0.2,Enum.EasingStyle.Elastic,Enum.EasingDirection.InOut,1,false)
	local flickOn = TS:Create(light,info,{Brightness = originalBrightness})
	local flickOff = TS:Create(light,info,{Brightness = 0})
	
	flickOff:Play()
	flickOff.Completed:Wait()
	light.Parent.Material = Enum.Material.SmoothPlastic
	
	flickOn:Play()
	flickOn.Completed:Wait()
	light.Parent.Material = Enum.Material.Neon
	
	flickOff:Play()
	flickOff.Completed:Wait()
	light.Parent.Material = Enum.Material.SmoothPlastic
end

function room.Blackout(Room)
	if Room and Room:FindFirstChild("Build") then
		local lamps = Room.Build.Lights:GetChildren()
		for i,lamp in ipairs(lamps) do
			for i,obj in ipairs(lamp:GetDescendants()) do
				if obj:IsA("Light") then
					task.spawn(function()
						room.FlickerLight(obj)
					end)
				end
			end
		end
	end
end

function room.GetRandom(prevRoom)
	local totalWeight = 0
	for i,info in pairs(room.info) do
		totalWeight += info.Weight
	end
	
	local randomWeight = room.Random:NextNumber(1,totalWeight)
	local currentWeight = 0
	local randomRoom = nil
	for i,info in pairs(room.info) do
		currentWeight += info.Weight
		if randomWeight <= currentWeight then
			randomRoom = game.ReplicatedStorage.Rooms[i]
			break
		end
	end
	
	--Next room direction must be diff than original room
	--if turn on way, turn the other way
	--if prev room had stairs, then next must have no stairs
	
	local Direction = room.info[randomRoom.Name]["Direction"]
	local hasStairs = room.info[randomRoom.Name]["Stairs"]
	local prevHadStairs = room.info[prevRoom.Name]["Stairs"]
	
	if prevRoom.Name == randomRoom.Name
		or Direction and Direction == room.lastTurnDirection
		or hasStairs and prevHadStairs
	then
		return room.GetRandom(prevRoom)
	else
		if Direction then
			room.lastTurnDirection = Direction
		end
		
		return randomRoom
	end
	
end

local probability = 0
local probabilityIncrease = 0.05

function room.Generate(prevRoom,specificRoomName)
	local newRoom = nil
	
	if specificRoomName and game.ReplicatedStorage.Rooms:FindFirstChild(specificRoomName) then
		newRoom = game.ReplicatedStorage.Rooms[specificRoomName]:Clone()
	else
		local randomRoom = room.GetRandom(prevRoom)
		newRoom = randomRoom:Clone()
	end
	
	if not newRoom:FindFirstChild("RoomNumber") then
		local RoomNumberVal = Instance.new("IntValue",newRoom)
		RoomNumberVal.Name = "RoomNumber"
	end
	if prevRoom:FindFirstChild("RoomNumber") then
		newRoom:FindFirstChild("RoomNumber").Value = prevRoom:FindFirstChild("RoomNumber").Value+1
	end
	
	if newRoom:FindFirstChild("Entrance") then
		newRoom.Entrance.Transparency = 1
	end
	if newRoom:FindFirstChild("Exit") then
		newRoom.Exit.Transparency = 1
	end
	if prevRoom:FindFirstChild("Entrance") then
		prevRoom.Entrance.Transparency = 1
	end
	if prevRoom:FindFirstChild("Exit") then
		prevRoom.Exit.Transparency = 1
	end
	newRoom.Parent = workspace.GeneratedRooms
	newRoom.PrimaryPart = newRoom.Entrance
	newRoom:PivotTo(prevRoom.Exit.CFrame)
	
	--SideRoomProbability--
	if prevRoom:FindFirstChild("SideRoom") then
		prevRoom.SideRoom:FindFirstChild("Entrance").Transparency = 1
		if room.Random:NextInteger(1,3) == 3 then
			--Picks a random room from the side room folder
			local randomSideRoom = GameAssets.SideRooms:GetChildren()[room.Random:NextInteger(1,#GameAssets.SideRooms:GetChildren())]:Clone()
			randomSideRoom.Name = "Room"
			randomSideRoom.Parent = prevRoom.SideRoom
			randomSideRoom:PivotTo(prevRoom.SideRoom.Entrance.CFrame)
			room.door.AddDoor(randomSideRoom,"SideDoor")
			randomSideRoom.Entrance.Transparency = 1
		else
			--spawn a broken door
			room.door.AddDoor(prevRoom.SideRoom,"BrokenDoor")
		end
	end
	
	--RandomDarkRoom--
	if room.Random:NextInteger(1,4) == 4  then
		if prevRoom:FindFirstChild("Build") and prevRoom.Build:FindFirstChild("Lights") then
			for i,lightPart in pairs(prevRoom.Build.Lights:GetChildren()) do
				if lightPart:FindFirstChildWhichIsA("Light") then
					lightPart:FindFirstChildWhichIsA("Light").Enabled = false
					lightPart.Material = Enum.Material.SmoothPlastic
				end
			end
		end
	end
	
	---Probability of getting a fleshCircuit room spawned
	local randomNumber = math.random()
	if randomNumber <= probability then
		--fleshCircuit can spawn
		if prevRoom:FindFirstChild("Furniture") then
			if prevRoom:FindFirstChild("SideRoom") then
				room.furniture.FurnishRoom(prevRoom.SideRoom:FindFirstChild("Room"))
			end
			room.furniture.FurnishRoom(prevRoom,true,3)
			local fleshCircuitSpawnValue = Instance.new("BoolValue",prevRoom)
			fleshCircuitSpawnValue.Name = "fleshCircuitSpawnValue"
			probability = 0
		end
	else
		room.furniture.FurnishRoom(prevRoom,false,2)
		if prevRoom:FindFirstChild("SideRoom") then
			room.furniture.FurnishRoom(prevRoom.SideRoom:FindFirstChild("Room"))
		end
	end
	
	if prevRoom:FindFirstChild("RoomNumber").Value>4 then
		if prevRoom:FindFirstChild("RoomNumber").Value%2==0 then
			probability = math.min(probability + probabilityIncrease, 1)
		end
	end
	
	
	---DoorTypeSpawn--
	if newRoom:FindFirstChild("Entrance") then
		if prevRoom:FindFirstChild("Furniture") then
			local LockedRandomizer = room.Random:NextInteger(2,2)
			if LockedRandomizer == 2 then
				--Lock Room
				
				local LockTypeRandomizer = room.Random:NextInteger(1,3)
				
				for _, item in ipairs(prevRoom:FindFirstChild("Furniture"):GetDescendants()) do
					if item.Name == "Location" and item:IsA("Attachment") then
						if LockTypeRandomizer == 1 then --Default Keycard Door
							room.door.AddDoor(prevRoom,"KeycardDoor",newRoom:FindFirstChild("RoomNumber").Value,true)
							break
						elseif LockTypeRandomizer == 2 then
							room.door.AddDoor(prevRoom,"ComputerDoor",newRoom:FindFirstChild("RoomNumber").Value,true)
							break
						end
					end
				end
				
				if LockTypeRandomizer == 3 and prevRoom:FindFirstChild("LeverSpawns") then
					--print("LeverRoom")
					room.door.AddDoor(prevRoom,"LeverDoor",newRoom:FindFirstChild("RoomNumber").Value,true)
					return
				end
			end
		end
		room.door.AddDoor(prevRoom,"Door",newRoom:FindFirstChild("RoomNumber").Value,false)
	end
	
	--print(prevRoom,newRoom)
	
	--[[
	if newRoom:FindFirstChild("Entrance") then
		if prevRoom:FindFirstChild("Furniture") then
			if room.Random:NextInteger(1,1) == 1 then
				for _, item in ipairs(prevRoom:FindFirstChild("Furniture"):GetChildren()) do
					if item.Name == "Drawer" or item.Name == "TallDrawer" then
						--Locked Room
						if room.Random:NextInteger(1,3)==1 then --Default Keycard Door
							--room.door.AddDoor(prevRoom,"KeycardDoor",newRoom:FindFirstChild("RoomNumber").Value,true)
							room.door.AddDoor(prevRoom,"LeverDoor",newRoom:FindFirstChild("RoomNumber").Value,true)
						elseif room.Random:NextInteger(1,3)==2 then
							--room.door.AddDoor(prevRoom,"ComputerDoor",newRoom:FindFirstChild("RoomNumber").Value,true)
							room.door.AddDoor(prevRoom,"LeverDoor",newRoom:FindFirstChild("RoomNumber").Value,true)
						elseif room.Random:NextInteger(1,3)==3 then
							room.door.AddDoor(prevRoom,"LeverDoor",newRoom:FindFirstChild("RoomNumber").Value,true)
						end
						
						break
						
					else
						room.door.AddDoor(prevRoom,"Door",newRoom:FindFirstChild("RoomNumber").Value,false) --Unlocked Room
						break
					end
				end
				
			else
				room.door.AddDoor(prevRoom,"Door",newRoom:FindFirstChild("RoomNumber").Value,false) --Unlocked Room
			end
		else
			room.door.AddDoor(prevRoom,"Door",newRoom:FindFirstChild("RoomNumber").Value,false)
		end
	end
	--]]
	return newRoom
end

return room

The exact same variables being used here causes some complex clashing which could be the issue.

I suggest you supply the room in the form of it’s existence in the generatedrooms table like previousRoom = room.Generate(generatedRooms[i-1])

Im checking, it seems like the main issue is with the new Doors system in this section:

if newRoom:FindFirstChild("Entrance") then
		if prevRoom:FindFirstChild("Furniture") then
			local LockedRandomizer = room.Random:NextInteger(2,2)
			if LockedRandomizer == 2 then
				--Lock Room
				
				local LockTypeRandomizer = room.Random:NextInteger(1,3)
				
				for _, item in ipairs(prevRoom:FindFirstChild("Furniture"):GetDescendants()) do
					if item.Name == "Location" and item:IsA("Attachment") then
						if LockTypeRandomizer == 1 then --Default Keycard Door
							room.door.AddDoor(prevRoom,"KeycardDoor",newRoom:FindFirstChild("RoomNumber").Value,true)
							break
						elseif LockTypeRandomizer == 2 then
							room.door.AddDoor(prevRoom,"ComputerDoor",newRoom:FindFirstChild("RoomNumber").Value,true)
							break
						end
					end
				end
				
				if LockTypeRandomizer == 3 and prevRoom:FindFirstChild("LeverSpawns") then
					--print("LeverRoom")
					room.door.AddDoor(prevRoom,"LeverDoor",newRoom:FindFirstChild("RoomNumber").Value,true)
					return
				end
			end
		end
		room.door.AddDoor(prevRoom,"Door",newRoom:FindFirstChild("RoomNumber").Value,false)
	end

This was the old system that caused some issues with spawning rooms, but it didnt cause the “returning nil” issue

yes sir, that’s correct! But im currently looking into it

Update: The problem is in here:

local LockTypeRandomizer = room.Random:NextInteger(1,3)

				for _, item in ipairs(prevRoom:FindFirstChild("Furniture"):GetDescendants()) do
					if item.Name == "Location" and item:IsA("Attachment") then
						if LockTypeRandomizer == 1 then --Default Keycard Door
							room.door.AddDoor(prevRoom,"KeycardDoor",newRoom:FindFirstChild("RoomNumber").Value,true)
							break
						elseif LockTypeRandomizer == 2 then
							room.door.AddDoor(prevRoom,"ComputerDoor",newRoom:FindFirstChild("RoomNumber").Value,true)
							break
						end
					end
				end

				if LockTypeRandomizer == 3 and prevRoom:FindFirstChild("LeverSpawns") then
					--print("LeverRoom")
					room.door.AddDoor(prevRoom,"LeverDoor",newRoom:FindFirstChild("RoomNumber").Value,true)
					return
				end

PROBLEM SOLVED:

if newRoom:FindFirstChild("Entrance") then
		if prevRoom:FindFirstChild("Furniture") then
			local LockedRandomizer = room.Random:NextInteger(2,2)
			if LockedRandomizer == 2 then
				--Lock Room
				print("Room locked",prevRoom)
				
				local LockTypeRandomizer = room.Random:NextInteger(1,3)
				if LockTypeRandomizer == 1 then
					room.door.AddDoor(prevRoom,"KeycardDoor",newRoom:FindFirstChild("RoomNumber").Value,true)
				elseif LockTypeRandomizer == 2 then
					if prevRoom:FindFirstChild("ComputerSpawns") then
						room.door.AddDoor(prevRoom,"ComputerDoor",newRoom:FindFirstChild("RoomNumber").Value,true)
					else
						room.door.AddDoor(prevRoom,"Door",newRoom:FindFirstChild("RoomNumber").Value,false)
					end
					
				elseif LockTypeRandomizer == 3 then
					if prevRoom:FindFirstChild("LeverSpawns") then
						room.door.AddDoor(prevRoom,"LeverDoor",newRoom:FindFirstChild("RoomNumber").Value,true)
					else
						room.door.AddDoor(prevRoom,"Door",newRoom:FindFirstChild("RoomNumber").Value,false)
					end
				end
				
			else
				room.door.AddDoor(prevRoom,"Door",newRoom:FindFirstChild("RoomNumber").Value,false)
			end
		else
			room.door.AddDoor(prevRoom,"Door",newRoom:FindFirstChild("RoomNumber").Value,false)
		end
	end

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