Nước gây thất bại
Bài học 10 trên 20 trong khóa học Trò chơi Roblox: sinh tồn trước thảm họa của Coddy.
Đến lúc tạo thảm họa đầu tiên. Trò chơi sẽ có một số thảm họa, nên chúng được đặt trong cùng một bảng, mỗi hàm nhận bản đồ làm tham số. Hàm được lưu trong bảng có cách gọi giống như các hàm khác:
local Disasters = {}
function Disasters.Flood(map)
-- Tạo nước
end
Disasters.Flood(map)Lũ bắt đầu bằng một phần màu xanh rộng nằm bên dưới hòn đảo. Phần này không được đỡ người chơi, nên CanCollide có giá trị false (sự kiện Touched vẫn kích hoạt), và nó luôn Anchored. Mỗi thảm họa sẽ hạ gục bất cứ ai nó chạm vào, nên ta đặt tên cho công việc đó:
local function defeat(hit)
local humanoid = hit.Parent:FindFirstChild("Humanoid")
if humanoid then
humanoid.Health = 0
end
end
lava.Touched:Connect(defeat)hit là phần đã chạm vào, chẳng hạn như bàn chân; Parent của nó là nhân vật. Đặt nước vào trong bản đồ, để khi phá hủy bản đồ vào cuối vòng chơi, nước cũng bị phá hủy theo.
Thử thách
Trung bìnhThêm thảm họa đầu tiên: nước khiến người chơi bị hạ gục.
- Viết
defeat(hit): nếuhit.Parentcó Humanoid, đặt Health của nó thành 0. - Tạo
local Disasters = {}vàDisasters.Flood(map): một partWatertrong bản đồ, được neo, CanCollide là false, rộng và dài hơnmap.Base10 stud, nằm thấp hơn 2 stud, Touched được kết nối vớidefeat. - Gọi
Disasters.Flood(map)sau khi dịch chuyển.
Nhấn Chơi rồi đi khỏi hòn đảo: nước bên dưới sẽ khiến bạn bị hạ gục. Sau đó nhấn Kiểm tra.
Tự làm thử
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")
-- Mỗi người chơi có Wins trên bảng xếp hạng
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)
-- Bảng trạng thái trong sảnh chờ
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
-- Bài học 10: Thêm Water That Defeats vào đây
-- Vòng lặp của các vòng chơi
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
Tất cả bài học trong Trò chơi Roblox: sinh tồn trước thảm họa
4Biến mọi thứ thành trò chơi
Hai bản đồ2Bản đồ và người chơi
Tải bản đồTự luyện tập: Playground Roblox