Keys and Locks
Lesson 8 of 20 in Coddy's Roblox Game: Escape the Dark course.
Some rooms are locked (the Locked template): their door has the attribute Locked, and the key is somewhere inside, at a KeySpot. The key is a Tool; pressing its prompt puts it in the player's Backpack.
A locked door opens only for a player carrying the key, in their hand (in their character) or in their Backpack. The door uses it up:
local key = findTool(player, "Key")
if not key then
return
end
key:Destroy()
door:SetAttribute("Locked", nil)Challenge
Medium- Write
findTool(player, name): the tool in their character, or in their Backpack. - In
addRoom, if the room has aKeySpot, put a clone ofServerStorage.Keythere (key.Handle.CFrame = spot.CFrame, parented to the room); itsHandle.Promptswitches itself off and moves the key into the player's Backpack. - In
setupDoor, before the door opens: a locked door needs the key, uses it up and loses itsLockedattribute.
Press Play and get through room 3. 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)
-- Walking into a room: it becomes the player's Room, and their checkpoint
local function setupEntry(room)
room.Entry.Touched:Connect(function(hit)
local player = Players:GetPlayerFromCharacter(hit.Parent)
if player then
local number = room:GetAttribute("Number")
player.leaderstats.Room.Value = number
player.RespawnLocation = room.Checkpoint
end
end)
end
setupEntry(workspace.Rooms.Lobby)
-- 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:SetAttribute("Number", count)
room.Sign.SurfaceGui.Number.Text = "Room " .. count
room.Parent = rooms
lastRoom = room
setupDoor(room.Door)
setupEntry(room)
-- keep the newest room and KeepRooms behind it
for _, old in rooms:GetChildren() do
if old:GetAttribute("Number") < count - Settings.KeepRooms then
old:Destroy()
end
end
return room
end
-- Every player: Room on the leaderboard, and a flashlight every life
Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local room = Instance.new("IntValue")
room.Name = "Room"
room.Parent = leaderstats
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)
This lesson includes a short quiz. Start the lesson to answer it and track your progress.