Menu
CoddyTech

Room Numbers

Lesson 6 of 20 in Coddy's Roblox Game: Escape the Dark course.

A player should know how deep they are. Every room gets its number: as an attribute, Number, and on the sign over its door (Sign.SurfaceGui.Number).

Each room has an invisible Entry part a step inside its entrance. When a player's body touches it, that room becomes their Room on the leaderboard and their checkpoint: a player's RespawnLocation is the SpawnLocation they come back at, and each room has one, Checkpoint.

player.RespawnLocation = room.Checkpoint

Players:GetPlayerFromCharacter(hit.Parent) says whose body touched it, or nil.

challenge icon

Challenge

Medium
  1. Give every player Room on the leaderboard (a leaderstats Folder with an IntValue).
  2. In addRoom, set the room's Number attribute to count and its sign to "Room " .. count (Room 1 for the first).
  3. Write setupEntry(room): when its Entry is touched by a player, set their Room to the room's number and their RespawnLocation to its Checkpoint. Call it for the Lobby and for every new room.

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

local addRoom -- written below: a door adds a room, and a room has a door

-- 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
		if door.Parent == lastRoom then
			addRoom()
		end
		openDoor(door)
	end)
end
setupDoor(workspace.Rooms.Lobby.Door)

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

-- 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