Menu
CoddyTech

获胜者姓名

Coddy Roblox 游戏:灾难生存 课程的第 9 课,共 20 课。

获胜者想让自己的名字闪耀在灯光下。要列出幸存者的名字,先把它们收集到列表中,再将它们拼接起来。

列表是一个键为 1、2、3 的表。table.insert(list, value) 会将元素添加到末尾,#list 表示它的长度,table.concat(list, ", ") 会将这些元素拼接成一个字符串,并在它们之间加上分隔符:

local names = {}
table.insert(names, "Ana")
table.insert(names, "Ben")
print(#names) --> 2
print(table.concat(names, ", ")) --> Ana, Ben

不能用 .. 将字符串与表拼接在一起,这会导致错误。先把列表拼接成字符串。

challenge icon

挑战

中等

在公告板上显示获胜者的名字。

  1. 回合结束时,创建 local names = {},并用 table.insert 添加每位幸存者的 Name。
  2. 显示 "Winners: " .. table.concat(names, ", ");如果 #names 为 0,则显示 Nobody survived!。

按下“开始”并在一回合中存活下来:公告板会显示你的名字作为获胜者。然后点击“检查”。

亲手试试

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

-- 回合循环
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

	for player in survivors do
		player.leaderstats.Wins.Value += 1
		teleport(player, lobbySpawn)
	end
	map:Destroy()
	-- 第 9 课:在这里添加获胜者名字
	if next(survivors) then
		setStatus("Survivors win!")
	else
		setStatus("Nobody survived!")
	end
	task.wait(3)
end
quiz icon自我测试

这节课包含一个小测验。开始学习即可作答,并记录你的进度。

Roblox 游戏:灾难生存中的所有课

自己练习: Roblox 在线编辑器