Players to arrays to text labels

Hello everyone, I recently got into a problem, I tried to make an array/table of all the players that are on a specific team and I want to make this table display as a text label, the idea is a round system but if the people survived/won they’ll be on the winners team and then there’ll be a table created of all the winner’s username and it’ll display as a text label: (for example: “The winners are: player1, player2, player3, etc…”)

What is the best way to do it and how can I do it?
Thanks :smiley:

Make a string with "The winners are: " then iterate through the table of usernames, concatenating each user name to the string e.g. stringName = stringName + userName + ", "
Then after that just set the textlabel text to the string.

If you have the table stored and setup at this point already, all you’d need to do is fire a remote event to every player using FireAllClients on the remote event object you create to populate the TextLabel on the clients’ GUI, passing the table of winners through the remote event, like so:

local winnersTable = {} -- This would be populated with winners at this point. This is also assuming they're the NAMES of the players (so strings, not the Player objects themselves).

-- You would call :FireAllClients() on this remote when you're ready to show the players the winners.
local showWinnersToPlayersRemote = location.of.remoteevent
showWinnersToPlayerRemotes:FireAllClients(winnersTable)

And on the client, you’d listen for this event in a LocalScript and populate the TextLabel’s text property, like so:

local winnersTextLabel = location.of.winners.textlabel

local showWinnersToPlayersRemote = location.of.remoteevent
showWinnersToPlayerRemotes.OnClientEvent:Connect(function(winnersTable)
    local winnersText = "The winners are: "

    for i = 1, #winnersTable do
        winnersText = winnersText .. winnersTable[i]
  
        -- We do this check so that we don't add a ', ' to the end of the winners list, since that wouldn't make sense.
        if i >= #winnersTable then
            winnersText = winnersText .. "!"
        else
            winnersText = winnersText .. ", "
        end
    end

    winnersTextLabel.Text = winnersText
end)
1 Like

you cant join strings with + in lua like in python, use ..

2 Likes

Oh yeah lol been doing to much python recently mb

Thank you! and I have a question for the winners table, how can I make it so that the winners table is every single player who’s in a specific team?

Building off of @xmthl’s code, you can just add an extra if statement when populating the table to check for that specific criteria:

local players = game:GetService("Players")
local teams = game:GetService("Teams")
local winnersTable = {}

-- Populate the table winnersTable.
for _, player in ipairs(players:GetPlayers()) do
    -- Assuming a team named "Winners" exists, check to see if the player's Team value matches.
    if player.Team == teams.Winners then
        table.insert(winnersTable, player.Name)
    end
end

-- You would call :FireAllClients() on this remote when you're ready to show the players the winners.
local showWinnersToPlayersRemote = location.of.remoteevent
showWinnersToPlayerRemotes:FireAllClients(winnersTable)
1 Like

You can use table.concat() function for this

table.concat(winners, ",")

This code will convert the winners to a string, seperated with commas

Follow @daisytheghostchild98’s code, they have made exactly what you need, assuming the team the winners are in is called ‘Winners’. Might be worth doing a :FindFirstChild(“Winners”) in game.Teams and checking if it exists as a sanity check to determine whether the object exists, like so (this would replace the top script, not the bottom one).

local players = game:GetService("Players")
local teams = game:GetService("Teams")
local winnersTable = {}

-- Populate the table winnersTable.
for _, player in ipairs(players:GetPlayers()) do

    -- Find the winners team and check if it exists, if not we can create one.
    local winnersTeam = teams:FindFirstChild("Winners")
    if not winnersTeam then
        -- Setup the object here.
        winnersTeam = Instance.new("Team")
        winnersTeam.Name = "Winners"
        winnersTeam.Parent = teams
    end

    -- Assuming a team named "Winners" exists, check to see if the player's Team value matches.
    if player.Team == teams.Winners then
        table.insert(winnersTable, player.Name)
    end
end

-- You would call :FireAllClients() on this remote when you're ready to show the players the winners.
local showWinnersToPlayersRemote = location.of.remoteevent
showWinnersToPlayerRemotes:FireAllClients(winnersTable)
2 Likes

Thank you so much, I managed to get it work, thanks alot!

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.