Menu
CoddyTech

Room Templates

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

Behind the lobby door there is nothing yet. The rooms are templates in ServerStorage.Rooms, each a Model with an invisible Entrance part (its PrimaryPart, so it is the model's pivot) and an Exit part where its door is.

Placing a room after another is one line: put the new room's pivot, its entrance, on the old room's exit:

local room = ServerStorage.Rooms.Hall:Clone()
room:PivotTo(lastRoom.Exit.CFrame)

Which template comes next is in Settings.Order, a list of names. A new room's door needs setupDoor too.

challenge icon

Challenge

Medium

Put a room behind the lobby door.

  1. Add locals rooms = workspace.Rooms, lastRoom = rooms.Lobby, count = 0.
  2. Write addRoom(): add 1 to count, clone ServerStorage.Rooms[Settings.Order[(count - 1) % #Settings.Order + 1]], pivot it to lastRoom.Exit.CFrame.
  3. Parent it to rooms, set lastRoom, setupDoor(room.Door), return it. Call addRoom() last.

Press Play and open the lobby door: a hall waits behind it. 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)

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

-- Lesson 4: Room Templates goes here

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