怪物来了
Coddy Roblox 游戏:逃离黑暗 课程的第 9 课,共 25 课。
现在来看怪物。ServerStorage.Shadow 是一个高大的黑色模型,带有 Humanoid,因此它会像任何 NPC 一样使用 Humanoid:MoveTo 行走。
当房间 Settings.MonsterFrom(4)打开时,怪物会从玩家身后进入:出现在最新房间往前数第二个房间的入口处。通过遍历房间,可以根据它的 Number 找到那个房间:
for _, room in rooms:GetChildren() do
if room:GetAttribute("Number") == count - 2 then
behind = room
end
end模型的枢轴位于中间,因此它会移动到入口上方 3.1 个 studs 处,站在地板上。它的 Humanoid 的 WalkSpeed 会设为 Settings.MonsterSpeed 的速度。
挑战
中等怪物会从你身后出现。
- 在顶部添加
local monster = nil。 - 在
addRoom上方编写spawnMonster():将ServerStorage.Shadow克隆到monster,位置设为房间count - 2的Entrance.CFrame + Vector3.new(0, 3.1, 0);WalkSpeed 设为Settings.MonsterSpeed;将它的父级设为 workspace。 - 在
addRoom中,当count == Settings.MonsterFrom时调用它。
按下“开始”并通过第 3 个房间:Shadow 会站在你身后两间房的位置。然后按“检查”。
亲手试试
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")
-- 玩家携带的工具:拿在手里,或放在 Backpack 中
local function findTool(player, name)
return (player.Character and player.Character:FindFirstChild(name)) or player.Backpack:FindFirstChild(name)
end
-- 让门绕着铰链旋转打开
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
-- 第 9 课:The Monster Comes 的内容写在这里
local addRoom -- 下面会编写:门会添加房间,房间里也有门
-- 玩家开门时,门会执行的操作
local function setupDoor(door)
door.Panel.Prompt.Triggered:Connect(function(player)
if door:GetAttribute("Open") then
return
end
if door:GetAttribute("Locked") then
local key = findTool(player, "Key")
if not key then
return
end
key:Destroy()
door:SetAttribute("Locked", nil)
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)
-- 上锁的房间里放着钥匙
local keySpot = room:FindFirstChild("KeySpot")
if keySpot then
local key = ServerStorage.Key:Clone()
key.Handle.CFrame = keySpot.CFrame
key.Parent = room
key.Handle.Prompt.Triggered:Connect(function(player)
key.Handle.Prompt.Enabled = false
key.Parent = player.Backpack
end)
end
-- 保留最新的房间,以及它后面的 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)
Roblox 游戏:逃离黑暗中的所有课
自己练习: Roblox 在线编辑器