Menu
CoddyTech

Mưa sao băng

Bài học 12 trên 20 trong khóa học Trò chơi Roblox: sinh tồn trước thảm họa của Coddy.

Thảm họa thứ hai thả một thiên thạch đang cháy xuống mỗi nửa giây trong suốt vòng chơi. Vòng lặp chờ sẽ làm chậm đồng hồ đếm ngược của vòng chơi, nên nó chạy song song với đồng hồ bằng task.spawn:

task.spawn(function()
	while map.Parent do
		-- thả một thiên thạch
		task.wait(0.5)
	end
end)

while map.Parent do tự dừng: khi vòng chơi hủy bản đồ, Parent của nó trở thành nil. Mỗi thiên thạch đáp xuống một vị trí ngẫu nhiên, cách tâm hòn đảo math.random(-15, 15) studs, và Debris:AddItem(meteor, 4) xóa nó sau 4 giây để chúng không chất đống.

Hiện tại, vòng chơi thả thiên thạch thay cho trận lụt. Bài học 13 sẽ cho vòng chơi tự chọn.

challenge icon

Thử thách

Trung bình

Thêm một trận mưa sao băng.

  1. Lấy dịch vụ Debris ở đầu.
  2. Viết Disasters.Meteors(map): trong một task.spawn, tạo vòng lặp while map.Parent do, với task.wait(0.5) ở mỗi lượt.
  3. Ở mỗi lượt, tạo một quả cầu không neo, kích thước 3 × 3 × 3, tên là Meteor trong bản đồ, ở độ cao 40 stud và cách map.Base không quá 15 stud; sự kiện Touched khiến nhân vật bị hạ; Debris:AddItem(meteor, 4).
  4. Gọi hàm này thay cho Disasters.Flood(map).

Nhấn Chơi: sao băng sẽ trút xuống hòn đảo suốt vòng chơi. Sau đó nhấn Kiểm tra.

Tự làm thử

local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local TweenService = game:GetService("TweenService")
local Settings = require(game.ReplicatedStorage.Settings)

local lobbySpawn = workspace.LobbySpawn

print("Rounds last " .. Settings.RoundLength .. " seconds")

-- Mỗi người chơi đều 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

local function defeat(hit)
	local humanoid = hit.Parent:FindFirstChild("Humanoid")
	if humanoid then
		humanoid.Health = 0
	end
end

-- Các thảm họa, phân theo tên
local Disasters = {}

function Disasters.Flood(map)
	local base = map.Base
	local water = Instance.new("Part")
	water.Name = "Water"
	water.Anchored = true
	water.CanCollide = false
	water.Transparency = 0.3
	water.Color = Color3.fromRGB(40, 120, 255)
	water.Size = Vector3.new(base.Size.X + 10, 1, base.Size.Z + 10)
	water.Position = base.Position - Vector3.new(0, 2, 0)
	water.Parent = map
	water.Touched:Connect(defeat)

	local rise = TweenInfo.new(Settings.RoundLength * 0.8, Enum.EasingStyle.Linear)
	TweenService:Create(water, rise, { Position = water.Position + Vector3.new(0, 8, 0) }):Play()
end

-- Bài học 12: Meteor Shower được thêm vào đây

-- Vòng lặp (loop) 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

	Disasters.Flood(map)
	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

Tự luyện tập: Playground Roblox