Menu
CoddyTech

One After Another

Lesson 5 of 25 in Coddy's Roblox Game: Escape the Dark course.

One room made at the start makes a game of one room. The game should make a room when a door opens: open the newest room's door, and a new room is behind it.

That gives two functions that need each other: setupDoor calls addRoom, and addRoom calls setupDoor. A local must be declared before code can use it, so addRoom is declared first and written later:

local addRoom -- declared here, written below

local function setupDoor(door)
	-- ... addRoom() ...
end

function addRoom()
	-- ... setupDoor(room.Door) ...
end

When the count reaches Settings.RoomsToEscape (10), the room is the Exit template, with a green door. For now it opens like any other; chapter 4 makes it the way out.

challenge icon

Challenge

Medium

Make a new room behind every door you open.

  1. Declare local addRoom above setupDoor and write it below as function addRoom(). Remove the addRoom() call at the end.
  2. In setupDoor, before opening: if door.Parent == lastRoom, call addRoom().
  3. In addRoom, use the "Exit" template once count >= Settings.RoomsToEscape.

Press Play and open three doors: a new room waits behind each one. Then Check.

Try it yourself

local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local Lighting = game:GetService("Lighting")
local TweenService = game:GetService("TweenService")
local Settings = require(game.ReplicatedStorage.Settings)

local rooms = workspace.Rooms
local lastRoom = rooms.Lobby -- the newest room
local count = 0 -- rooms made so far

-- Night falls: no sun, a little light everywhere, and fog
Lighting.ClockTime = Settings.ClockTime
Lighting.Ambient = Settings.Ambient
Lighting.OutdoorAmbient = Settings.Ambient
Lighting.FogColor = Color3.new(0, 0, 0)
Lighting.FogEnd = Settings.FogEnd
print("Lights out")

-- Swing a door open on its hinge
local function openDoor(door)
	door:SetAttribute("Open", true)
	door.Panel.Prompt.Enabled = false
	local hinge, panel = door.Hinge, door.Panel
	local goal = hinge.CFrame * CFrame.Angles(0, math.rad(90), 0) * hinge.CFrame:ToObjectSpace(panel.CFrame)
	TweenService:Create(panel, TweenInfo.new(0.6), { CFrame = goal }):Play()
end

-- What a door does when a player opens it
local function setupDoor(door)
	door.Panel.Prompt.Triggered:Connect(function(player)
		if door:GetAttribute("Open") then
			return
		end
		-- Lesson 5: One After Another goes here
		openDoor(door)
	end)
end
setupDoor(workspace.Rooms.Lobby.Door)

-- A new room at the exit of the newest one
local function addRoom()
	count += 1
	local name = Settings.Order[(count - 1) % #Settings.Order + 1]
	local room = ServerStorage.Rooms[name]:Clone()
	room:PivotTo(lastRoom.Exit.CFrame)
	room.Parent = rooms
	lastRoom = room
	setupDoor(room.Door)
	return room
end

addRoom()

-- Every player: a flashlight every life
Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function()
		local flashlight = ServerStorage.Flashlight:Clone()
		flashlight.Parent = player.Backpack
		local beam = flashlight.Handle.Beam
		flashlight.Activated:Connect(function()
			beam.Enabled = not beam.Enabled
		end)
	end)
end)

All lessons in Roblox Game: Escape the Dark

Practice on your own: Roblox playground