Menu
CoddyTech

Winner Names

Lesson 9 of 18 in Coddy's Roblox Game: Disaster Survival course.

A winner wants to see their name in lights. To list the survivors' names, collect them in a list and join it.

A list is a table with keys 1, 2, 3. table.insert(list, value) adds to the end, #list is its length, and table.concat(list, ", ") joins the items into one string with a separator between them:

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

A string cannot be joined to a table with ..; that is an error. Join the list into a string first.

challenge icon

Challenge

Medium

Name the winners on the board:

  1. When the round ends, make an empty list names and add each survivor's Name to it.
  2. Show Winners: followed by the names joined with , , such as Winners: Player1, or Nobody survived! when the list is empty.

Press Play and survive a round, then Check.

Try it yourself

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")

-- Every player gets Wins on the leaderboard
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)

-- The status board in the lobby
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

-- The round loop
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()
	if next(survivors) then
		setStatus("Survivors win!")
	else
		setStatus("Nobody survived!")
	end
	task.wait(3)
end
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Roblox Game: Disaster Survival