Menu
CoddyTech

One After Another

Lesson 5 of 20 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, the room is the Exit template: the way out.

challenge icon

Challenge

Medium
  1. Declare local addRoom above setupDoor, and write it as function addRoom().
  2. In setupDoor, before opening: if the door belongs to lastRoom, add a room.
  3. In addRoom, use the Exit template once count reaches Settings.RoomsToEscape.
  4. Remove the addRoom() call at the end.

This lesson's world has a short way out (3 rooms). Press Play, 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
		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)
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Roblox Game: Escape the Dark