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