How do I make a room spawn in the 1000th room?

I’m making the room to always spawn in the 1000th room (Just like Doors would).

But I don’t know how.

Here is the script.
Server:

local room = require(script.Room)
local door = require(script.Room.Door)

local prevRoom = workspace.StartRoom
local firstDoor = door.New(prevRoom, 0)

for i = 1, 1000 do
	prevRoom = room.Generate(prevRoom, i)
end

Here are the module scripts.
Room:

local door = require(script.Door)

local room = {}
room.info = require(script.RoomInfo)
room.lastTurnDirection = nil
room.random = Random.new()

function room.GetRandom(prevRoom)
	local totalRarity = 0
	for i, info in pairs(room.info) do
		totalRarity += info.Rarity
	end
	
	local randomRarity = room.random:NextNumber(0, totalRarity)
	local currentRarity = 0
	local randomRoom = nil
	for i, info in pairs(room.info) do
		currentRarity += info.Rarity
		if randomRarity <= currentRarity then
			randomRoom = workspace.Rooms[i]
			break
		end
	end
	
	local direction = room.info[randomRoom.Name]["Direction"]
	
	if (prevRoom.Name == randomRoom.Name)
		or (direction and direction == room.lastTurnDirection) 
	then
		return room.GetRandom(prevRoom)
	else
		if direction then
			room.lastTurnDirection = direction
		end
		return randomRoom
	end
end

function room.Generate(prevRoom, number)
	local randomRoom = room.GetRandom(prevRoom)
	local newRoom = randomRoom:Clone()
	
	newRoom.PrimaryPart = newRoom.Entrance
	newRoom:PivotTo(prevRoom.Exit.CFrame)
	
	local newDoor = door.New(newRoom, number)
	
	newRoom.Parent = workspace.GeneratedRooms
	
	return newRoom
end

return room

RoomInfo:

local roomInfo = {
	["StartRoom"] = {
		["Rarity"] = 0
	},
	["Room"] = {
		["Rarity"] = 20
	},
	["Hallway"] = {
		["Rarity"] = 20
	},
	["LongHallway"] = {
		["Rarity"] = 10
	},
	["LeftTurnHallway"] = {
		["Direction"] = "Left",
		["Rarity"] = 10
	},
	["RightTurnHallway"] = {
		["Direction"] = "Right",
		["Rarity"] = 9
	},
	["RoomWithActiveWindow"] = {
		["Rarity"] = 1
	},
	["RoomWithInactiveWindow"] = {
		["Rarity"] = 10
	},
	["RightTurnRoom"] = {
		["Direction"] = "Right",
		["Rarity"] = 10
	},
	["LeftTurnRoom"] = {
		["Direction"] = "Left",
		["Rarity"] = 10
	},
}
return roomInfo

Door:

local TweenService = game:GetService("TweenService")

local door = {}

function door.Open(doorModel)
	doorModel:SetAttribute("Open", true)
	
	local opencframe = doorModel.Hinge.CFrame * CFrame.Angles(0, math.rad(100), 0)
	local doorTween = TweenService:Create(doorModel.Hinge, TweenInfo.new(0.5), {CFrame = opencframe})
	
	doorTween:Play()
	doorModel.Door.Open:Play()
end

function door.New(roomModel, number)
	local doorModel = game.ReplicatedStorage.Door:Clone()
	
	doorModel:PivotTo(roomModel.Exit.CFrame)
	doorModel:SetAttribute("Open", false)
	
	doorModel.Sign.SurfaceGui.TextLabel.Text = "Funroom "..number
	
	doorModel.Sensor.Touched:Connect(function(hit)
		local humanoid = hit.Parent:FindFirstChild("Humanoid")
		if humanoid and not doorModel:GetAttribute("Open") then
			door.Open(doorModel)
		end
	end)
	
	doorModel.Parent = roomModel
	
	return doorModel
end

return door

Any help is appreciated, thanks.

3 Likes

Check the i in your numerical loop. If it’s equal to 1000, then spawn the room you want. Else, it will generate a random every other number.

for i = 1, 1000 do
    if i == 1000 then
        --generate specific room
    else
        prevRoom = room.Generate(prevRoom, i)
    end
end
2 Likes
local door = require(script.Door)

local room = {}
room.info = require(script.RoomInfo)
room.lastTurnDirection = nil
room.random = Random.new()

function room.GetRandom(prevRoom,specialroom)
-- now you can see if its time for the special room.
	local totalRarity = 0
	for i, info in pairs(room.info) do
		totalRarity += info.Rarity
	end
	
	local randomRarity = room.random:NextNumber(0, totalRarity)
	local currentRarity = 0
	local randomRoom = nil
	for i, info in pairs(room.info) do
		currentRarity += info.Rarity
		if randomRarity <= currentRarity then
			randomRoom = workspace.Rooms[i]
			break
		end
	end
	
	local direction = room.info[randomRoom.Name]["Direction"]
	
	if (prevRoom.Name == randomRoom.Name)
		or (direction and direction == room.lastTurnDirection) 
	then
		return room.GetRandom(prevRoom)
	else
		if direction then
			room.lastTurnDirection = direction
		end
		return randomRoom
	end
end

function room.Generate(prevRoom, number)
	local randomRoom = room.GetRandom(prevRoom,number == 1000)
	local newRoom = randomRoom:Clone()
	
	newRoom.PrimaryPart = newRoom.Entrance
	newRoom:PivotTo(prevRoom.Exit.CFrame)
	
	local newDoor = door.New(newRoom, number)
	
	newRoom.Parent = workspace.GeneratedRooms
	
	return newRoom
end

return room
2 Likes

How do I spawn my specific room in the 1000th room?

1 Like
local thousandthroom = -- path to special room
function room.GetRandom(prevRoom,specialroom)
	if specialroom then 
		return thousandthroom 
	end
	local totalRarity = 0
	for i, info in pairs(room.info) do
		totalRarity += info.Rarity
	end
	
	local randomRarity = room.random:NextNumber(0, totalRarity)
	local currentRarity = 0
	local randomRoom = nil
	for i, info in pairs(room.info) do
		currentRarity += info.Rarity
		if randomRarity <= currentRarity then
			randomRoom = workspace.Rooms[i]
			break
		end
	end
	
	local direction = room.info[randomRoom.Name]["Direction"]
	
	if (prevRoom.Name == randomRoom.Name)
		or (direction and direction == room.lastTurnDirection) 
	then
		return room.GetRandom(prevRoom)
	else
		if direction then
			room.lastTurnDirection = direction
		end
		return randomRoom
	end
end
2 Likes

Ummm, There’s a problem, The room appears after the 999th door and not the 1000th door.

2 Likes

So just add the 1000th door? I don’t understand. Provide images.

2 Likes

Here is the image of the problem:

1 Like

So the problem is that the door is not spawning correctly? Or the door is wrong? If door is wrong then just do the same thing I did for the room but now for the door. If not spawning correctly. Fix the values.

2 Likes

There should be a random room after the 999th door and the special room after the 1000th door.

Module Script:

local door = require(script.Door)
local thousandthroom = workspace.EndRoom

local room = {}
room.info = require(script.RoomInfo)
room.lastTurnDirection = nil
room.random = Random.new()

function room.GetRandom(prevRoom, specialroom)
	if specialroom then 
		return thousandthroom 
	end
	local totalRarity = 0
	for i, info in pairs(room.info) do
		totalRarity += info.Rarity
	end
	
	local randomRarity = room.random:NextNumber(0, totalRarity)
	local currentRarity = 0
	local randomRoom = nil
	for i, info in pairs(room.info) do
		currentRarity += info.Rarity
		if randomRarity <= currentRarity then
			randomRoom = workspace.Rooms[i]
			break
		end
	end
	
	local direction = room.info[randomRoom.Name]["Direction"]
	
	if (prevRoom.Name == randomRoom.Name)
		or (direction and direction == room.lastTurnDirection) 
	then
		return room.GetRandom(prevRoom)
	else
		if direction then
			room.lastTurnDirection = direction
		end
		return randomRoom
	end
end

function room.Generate(prevRoom, number)
	local randomRoom = room.GetRandom(prevRoom, number == 1000)
	local newRoom = randomRoom:Clone()
	
	newRoom.PrimaryPart = newRoom.Entrance
	newRoom:PivotTo(prevRoom.Exit.CFrame)
	
	local newDoor = door.New(newRoom, number)
	
	newRoom.Parent = workspace.GeneratedRooms
	
	return newRoom
end

return room

Script:

local room = require(script.Room)
local door = require(script.Room.Door)

local prevRoom = workspace.StartRoom
local firstDoor = door.New(prevRoom, 0)

for i = 1, 1000 do
	prevRoom = room.Generate(prevRoom, i)
end

The end room don’t have the exit. So how to make this?

2 Likes

So a special room after the 1000th room or after the loop finishes. For that why not just run the same function? room.Generate

2 Likes

How?

mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm

2 Likes
local door = require(script.Door)
local thousandthroom = workspace.EndRoom

local room = {}
room.info = require(script.RoomInfo)
room.lastTurnDirection = nil
room.random = Random.new()

function room.GetRandom(prevRoom)
	local totalRarity = 0
	for i, info in pairs(room.info) do
		totalRarity += info.Rarity
	end
	
	local randomRarity = room.random:NextNumber(0, totalRarity)
	local currentRarity = 0
	local randomRoom = nil
	for i, info in pairs(room.info) do
		currentRarity += info.Rarity
		if randomRarity <= currentRarity then
			randomRoom = workspace.Rooms[i]
			break
		end
	end
	
	local direction = room.info[randomRoom.Name]["Direction"]
	
	if (prevRoom.Name == randomRoom.Name)
		or (direction and direction == room.lastTurnDirection) 
	then
		return room.GetRandom(prevRoom)
	else
		if direction then
			room.lastTurnDirection = direction
		end
		return randomRoom
	end
end

function room.Generate(prevRoom, number)
	local randomRoom = room.GetRandom(prevRoom)
	if number > 1000 then
		randomRoom =  thousandthroom
	end
	local newRoom = randomRoom:Clone()
	
	newRoom.PrimaryPart = newRoom.Entrance
	newRoom:PivotTo(prevRoom.Exit.CFrame)
	
	local newDoor = door.New(newRoom, number)
	
	newRoom.Parent = workspace.GeneratedRooms
	
	return newRoom
end

return room
local room = require(script.Room)
local door = require(script.Room.Door)

local prevRoom = workspace.StartRoom
local firstDoor = door.New(prevRoom, 0)

for i = 1, 1001 do
	prevRoom = room.Generate(prevRoom, i)
end

I hope you understand where to put the scripts.

1 Like

Ummm, I’m getting this error: Exit is not a valid member of Model "EndRoom"

2 Likes

As you see it says Exit is not a valid member of Model "EndRoom" meaning Exit door does not exist. Check if it does exist and where does this error occurs.

2 Likes

As you can see, I want the end room to appear without the exit, but How?

1 Like

Then just not spawn it?

function room.Generate(prevRoom, number)
	local randomRoom = room.GetRandom(prevRoom)
	if number > 1000 then
		randomRoom =  thousandthroom
	end
	local newRoom = randomRoom:Clone()
	
	newRoom.PrimaryPart = newRoom.Entrance
	newRoom:PivotTo(prevRoom.Exit.CFrame)
	
	if number <= 1000 then
		local newDoor = door.New(newRoom, number)
	end
	
	newRoom.Parent = workspace.GeneratedRooms
	
	return newRoom
end

That fix doesn’t do anything, it just gives the same error.

1 Like

Oh its because the previous room lacks the exit. Does the 1000th room have a exit or no?

You probably have to split up your generate function.
Having only one function that generates a room randomly makes it impossible to make special rooms without cluttering up the whole generate function.