克敌之水
Coddy Roblox 游戏:灾难生存 课程的第 10 课,共 20 课。
第一个灾难来了。游戏中会有好几种灾难,所以把它们放在同一个表里,每种灾难都是一个接收地图的函数。存放在表中的函数和其他函数一样调用:
local Disasters = {}
function Disasters.Flood(map)
-- 生成水
end
Disasters.Flood(map)洪水一开始是岛屿下方的一大片蓝色部件。它不能托住玩家,所以 CanCollide 为 false(Touched 仍会触发),并且保持 Anchored。每种灾难都会击败它接触到的玩家,所以给这个任务起个名字:
local function defeat(hit)
local humanoid = hit.Parent:FindFirstChild("Humanoid")
if humanoid then
humanoid.Health = 0
end
end
lava.Touched:Connect(defeat)hit 是接触到的部件,例如一只脚;它的 Parent 是角色。把水设为地图的子对象,这样回合结束时销毁地图,也会一并销毁水。
挑战
中等添加第一个灾难:会击败玩家的水。
- 编写
defeat(hit):如果hit.Parent有 Humanoid,就将它的 Health 设为 0。 - 创建
local Disasters = {}和Disasters.Flood(map):在地图中创建一个名为Water的部件,将它锚定,并将 CanCollide 设为 false。让它比map.Base宽 10 studs、长 10 studs,并位于它下方 2 studs,然后将 Touched 连接到defeat。 - 传送后调用
Disasters.Flood(map)。
按下 Play,然后走下岛屿:岛下的水会击败你。然后点击 Check。
亲手试试
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local Settings = require(game.ReplicatedStorage.Settings)
local lobbySpawn = workspace.LobbySpawn
print("Rounds last " .. Settings.RoundLength .. " seconds")
-- 每位玩家的排行榜上都有 Wins
Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local wins = Instance.new("IntValue")
wins.Name = "Wins"
wins.Parent = leaderstats
end)
-- 大厅里的状态板
local gui = Instance.new("SurfaceGui")
gui.Parent = workspace.StatusBoard
local status = Instance.new("TextLabel")
status.Name = "Status"
status.Size = UDim2.fromScale(1, 1)
status.BackgroundColor3 = Color3.fromRGB(25, 25, 35)
status.TextColor3 = Color3.fromRGB(255, 255, 255)
status.TextScaled = true
status.Text = "Welcome to Disaster Survival!"
status.Parent = gui
local function setStatus(text)
status.Text = text
end
local function teleport(player, target)
local character = player.Character
if character then
character:PivotTo(target.CFrame + Vector3.new(0, 4, 0))
end
end
-- 第 10 课:在这里添加击败玩家的水
-- 回合循环
while true do
for seconds = Settings.Intermission, 1, -1 do
setStatus("Intermission: " .. seconds)
task.wait(1)
end
local map = ServerStorage.Maps.Island:Clone()
map.Name = "Map"
map.Parent = workspace
local survivors = {}
for _, player in Players:GetPlayers() do
local character = player.Character
if character and character.Humanoid.Health > 0 then
teleport(player, map.Spawn)
survivors[player] = true
character.Humanoid.Died:Connect(function()
survivors[player] = nil
end)
end
end
for seconds = Settings.RoundLength, 1, -1 do
setStatus("Time left: " .. seconds)
task.wait(1)
end
local names = {}
for player in survivors do
player.leaderstats.Wins.Value += 1
teleport(player, lobbySpawn)
table.insert(names, player.Name)
end
map:Destroy()
if #names > 0 then
setStatus("Winners: " .. table.concat(names, ", "))
else
setStatus("Nobody survived!")
end
task.wait(3)
end
Roblox 游戏:灾难生存中的所有课
自己练习: Roblox 在线编辑器