Menu
CoddyTech

钥匙和锁

Coddy Roblox 游戏:逃离黑暗 课程的第 8 课,共 25 课。

有些房间是锁着的(Locked 模板):它们的门有 Locked 属性,钥匙在房间里的某个 KeySpot。钥匙是一个 Tool;按下它的提示后,钥匙就会放进玩家的 Backpack。

只有携带钥匙的玩家才能打开锁着的门,钥匙可以拿在手上(在角色身上),也可以放在 Backpack 里。开门时会消耗钥匙:

local key = findTool(player, "Key")
if not key then
	return
end
key:Destroy()
door:SetAttribute("Locked", nil)
challenge icon

挑战

中等

找到对应的钥匙后,才能打开锁着的门。

  1. findTool(player, name):工具在玩家的 Character 中,否则就在 Backpack 中。
  2. addRoom 会在带有 KeySpot 的房间中放入一个 ServerStorage.Key 的克隆:key.Handle.CFrame = keySpot.CFrame。
  3. 它的 Handle.Prompt 会关闭,并将钥匙移到玩家的 Backpack 中。
  4. 在 setupDoor 中,带有 Locked 属性的门需要钥匙:销毁钥匙,并清除 Locked。

点击“开始”并到达第 3 个房间:那里有一扇需要钥匙才能打开的门。然后点击“检查”。

亲手试试

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 -- 最新的房间
local count = 0 -- 目前已创建的房间数

-- 夜幕降临:没有阳光,四处只有微光,还有雾
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")

-- 让门绕着铰链转开
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 -- 下面定义:一扇门会添加一个房间,而房间里也有一扇门

-- 玩家开门时,门会做什么
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)

-- 走进房间时:它会成为玩家的 Room,也是玩家的重生点
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)

-- 在最新房间的出口处添加新房间
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)
	-- 第 8 课:钥匙与锁的内容写在这里
	-- 保留最新房间及其后面的 KeepRooms 个房间
	for _, old in rooms:GetChildren() do
		local number = old:GetAttribute("Number")
		if number < count - Settings.KeepRooms then
			print("Room " .. number .. " is gone")
			old:Destroy()
		end
	end
	return room
end

-- 每位玩家:排行榜上显示 Room,每次重生都获得手电筒
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)
quiz icon自我测试

这节课包含一个小测验。开始学习即可作答,并记录你的进度。

Roblox 游戏:逃离黑暗中的所有课

自己练习: Roblox 在线编辑器