Menu
CoddyTech

它在行走

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

Shadow 一动不动。它的思维就像一个循环:每隔四分之一秒,就朝最近的存活玩家走去。这个循环永远不会结束,所以它会通过 task.spawn 在自己的线程中运行,而 addRoom 会同时继续执行。

local function runMonster(shadow)
	while shadow.Parent do
		-- 找到一个人,朝他走过去
		task.wait(0.25)
	end
end

task.spawn(runMonster, monster)

怪物被销毁后,while shadow.Parent 就会停止循环。

challenge icon

挑战

中等

让 Shadow 朝你走来。

  1. 编写 nearestCharacter(position):找出最近的存活玩家角色,以及它离得有多远。
  2. 编写 runMonster(shadow):当 shadow.Parent 存在时,将角色移动到最近角色的 HumanoidRootPart.Position,然后执行 task.wait(0.25)。
  3. 在 spawnMonster 的末尾,调用 task.spawn(runMonster, monster)。

按“开始”并通过第 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 -- 目前已创建的房间数
local monster = nil

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

-- 第 10 课:It Walks 的代码写在这里

-- 怪物从你身后出现:位置是最新房间往前数第 2 个房间的入口
local function spawnMonster()
	local behind = nil
	for _, room in rooms:GetChildren() do
		if room:GetAttribute("Number") == count - 2 then
			behind = room
		end
	end
	if not behind then
		return
	end
	monster = ServerStorage.Shadow:Clone()
	monster:PivotTo(behind.Entrance.CFrame + Vector3.new(0, 3.1, 0))
	monster.Humanoid.WalkSpeed = Settings.MonsterSpeed
	monster.Parent = workspace
end

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
	if count == Settings.MonsterFrom then
		spawnMonster()
	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 在线编辑器