闪烁
Coddy Roblox 游戏:逃离黑暗 课程的第 12 课,共 25 课。
警告:怪物附近的灯会闪烁。这只是视觉效果,所以应该放在 DarkClient 中,也就是 HUD 里的 LocalScript 中。在这里会发生一件有用的事:LocalScript 修改服务器上的部件时,只会在该玩家的屏幕上修改它。服务器上的灯不会改变。
每十分之一秒,遍历房间中的每个 LampLight,怪物 Settings.FlickerDistance 范围内的灯有 40% 的时间会亮着;其他灯则会:
light.Enabled = not near or math.random() < 0.4挑战
中等让 Shadow 附近的灯在你的屏幕上闪烁。
- 在
DarkClient中,使用require(game.ReplicatedStorage:WaitForChild("Settings"))。 - 启动一个
task.spawn循环,每隔 0.1 秒查找workspace.Shadow的根部件(如果它存在)。 - 对于
workspace.Rooms中的每个LampLight:设置light.Enabled = not near or math.random() < 0.4,其中 near 表示距离light.Parent不超过Settings.FlickerDistance。
按下“开始”,等 Shadow 出现:它附近的灯会闪烁。然后点击“检查”。
亲手试试
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local Lighting = game:GetService("Lighting")
local TweenService = game:GetService("TweenService")
local PathfindingService = game:GetService("PathfindingService")
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
-- MonsterSight 范围内最近的存活玩家,以及距离
local function nearestCharacter(position)
local best, bestDistance = nil, Settings.MonsterSight
for _, player in Players:GetPlayers() do
local character = player.Character
local root = character and character:FindFirstChild("HumanoidRootPart")
if root and character.Humanoid.Health > 0 then
local distance = (root.Position - position).Magnitude
if distance < bestDistance then
best, bestDistance = character, distance
end
end
end
return best, bestDistance
end
-- 朝目标点移动:路通畅就直走,否则沿路径走
local function walkToward(shadow, goal, path)
local root = shadow.HumanoidRootPart
local params = RaycastParams.new()
params.FilterDescendantsInstances = { shadow }
local hit = workspace:Raycast(root.Position, goal - root.Position, params)
if not hit or hit.Instance.Parent:FindFirstChildOfClass("Humanoid") then
shadow.Humanoid:MoveTo(goal)
return
end
path:ComputeAsync(root.Position, goal)
local points = path:GetWaypoints()
if path.Status == Enum.PathStatus.Success and points[2] then
shadow.Humanoid:MoveTo(points[2].Position)
end
end
-- 怪物的行动逻辑:每隔四分之一秒追逐一名玩家
local function runMonster(shadow)
local humanoid = shadow.Humanoid
local root = shadow.HumanoidRootPart
local path = PathfindingService:CreatePath()
while shadow.Parent do
local character = nearestCharacter(root.Position)
if character then
walkToward(shadow, character.HumanoidRootPart.Position, path)
else
humanoid:MoveTo(root.Position)
end
task.wait(0.25)
end
end
-- 怪物从你身后出现:位于最新房间往前数第 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
task.spawn(runMonster, monster)
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 在线编辑器