Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

List survivors after round?

I'm working on a game where players have to survive rising lava. How do I have a text at the top of the screen saying the names of all the players that survived the round? Thanks

2 comments

0

You would make a table, and insert all the names of the players who survive.

0

And no, I dont literally mean a table. Here's an explanation https://developer.roblox.com/en-us/api-reference/lua-docs/table

2 answers

✓ Accepted answer

An alternative solution that doesn't include using teams.

  • We will first create a table which will hold all the players that are alive.
  • If a player dies, it will trigger Humanoid.Died event which will remove that player from the playersAlive table.
local Players = game:GetService('Players')
local playersAlive = {}

function removeFromTable(username)
	for i = 1,#playersAlive do
		if (playersAlive[i] == username) then
			table.remove(playersAlive,i)
			break
		end
	end
end

function resetPlayers()
	-- Clear previous players
	playersAlive = {}

	-- Add new players
	for _,v in pairs(Players:GetChildren())do
		table.insert(self,v.Name)
	end
end

Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function()
		player.Character.Humanoid.Died:Connect(function()
			local username = player.Name		
			removeFromTable(username)
		end)
	end)
end)


resetPlayers()

Whenever the game starts, you call the resetPlayers. This function will add all players into the table.

When the game is over and you want to get all the players who survived, you simple iterate over the playersAlive table.

Note: I haven't tested this script so there might be some bugs.

Like the guy who said in the comments, you will need to make a table of the survivors. We can do this by going

game.Teams.Survivors:GetPlayers()

This will return a table of all the players on that team. Now, we will need to add these into a text label. We can do this by using a for i,v loop. This will loop through all the children in the table.

for i,v in pairs(game.Teams.Survivors:GetPlayers()) do
	
end

When we run this, it will still do nothing. What we need to do now is actually add the code in. What we can do is concatenate (combine string values) the label's text and the player's name into the label. We can do this by going

for i,v in pairs(game.Teams.survivors:GetPlayers()) do
	textLabel = script.Parent.TextLabel
	textLabel.Text = textLabel.Text.." ".. v.name..","
end

If we were to run this, it would work, but we can make it a little bit better. At the end of the for loop, we can add something else to make it make more sense.

for i,v in pairs(game.Teams.survivors:GetPlayers()) do
	textLabel = script.Parent.TextLabel
	textLabel.Text = textLabel.Text.." ".. v.name..","
end
textLabel.Text = textLabel.Text.. " have all survived."

(Modify the script for it to work with your explorer/hierarchy)

1 comment

0

Is there any way to do this without teams? I have seen other games that have done this without the need for teams.

This discussion is preserved as it was. New answers, comments, and votes are closed.