Menu
CoddyTech

Người chiến thắng

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

Sống sót qua một vòng chơi được tính là một lần thắng. Khi vòng chơi kết thúc, mỗi người chơi vẫn còn trong survivors sẽ nhận được một lần thắng:

for player in survivors do
	player.leaderstats.Wins.Value += 1
end

+= cộng thêm vào giá trị ngay tại chỗ. Để biết có ai sống sót hay không, next(survivors) trả về khóa đầu tiên của bảng, hoặc nil nếu bảng trống.

Người chơi cần một chút thời gian để đọc kết quả trước khi đồng hồ đếm ngược ở sảnh chờ đưa bảng trở lại, vì vậy vòng chơi kết thúc bằng task.wait(3).

challenge icon

Thử thách

Trung bình

Cộng 1 chiến thắng cho mỗi người sống sót.

  1. Khi vòng chơi kết thúc, cộng 1 vào Wins của mỗi người sống sót thay vì đếm họ.
  2. Sau khi bản đồ bị phá hủy, hiển thị Survivors win! nếu có người sống sót, nếu không thì hiển thị Nobody survived!
  3. Sau đó chạy task.wait(3) trước khi bắt đầu đếm ngược ở sảnh chờ tiếp theo.

Nhấn Chơi và sống sót qua một vòng chơi: Wins tăng lên 1 và bảng hiển thị Survivors win. 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

-- Vòng lặp các lượt 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 count = 0
	for player in survivors do
		count += 1
		teleport(player, lobbySpawn)
	end
	map:Destroy()
	-- Bài học 8: Thêm Winners vào đây
	print("Survivors: " .. count)
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