Splitting Multiple Player Names

do ya’ll know how could i do something like this?
for example:

Survivors: Evan, Ivan, Michael, Paulo, Pablo.

here’s my code so far also
ps: im just a beginner

for i, char in pairs(survivors) do
				if char then
					Status.Value = "Survivors "string.split(i, Splitter)
				end
			end

What do you mean exactly?.. Do you have a table of Player’s that survived, and you want to print them all in one sentence, with “Survivors:” printed in front?

yeah something like this Evan, Ivan, Michael, Paulo, Pablo.

You can use .concat on a table

local Table = {"Charles", "Richard", "Leopold"}
local newString = "Survivors: "..table.concat(Table, ", ")
print(newString)

image
image
To break down “concat”, it takes the table, takes one value at a time (Charles then Richard ect. ect), seperates them in this case by a comma and a space, since we said ", ". You can make any seperator, it could even be " - "

3 Likes

Something like this?

Status.Value = 'Survivors: '

for i, char in pairs(survivors) do
    if char then
        Status.Value ..= char.Name

        if i < #survivors then
            Status.Value ..= ', '
        end
    end
end
1 Like

Oh thats cool! I didn’t know about that.

tysm for this i thought string.split actually for splitting names wish you’ll help more beginner devs

Glad that I could help you out. If my reply was the solution, feel free to mark it so, so people know this question was solved.

btw i got an error concatenating an arrays
image
here’s the code:

Status.Value = "Survivors: "..table.concat(survivors, ", ")

I assume that is because, you have inserted the players into the table, and not the player names. So first you gotta convert the player instances into player names. :slight_smile:

ohh didn’t know that i could insert playersname in a table

but doesn’t that still returns an array?

Player.Names are actually just a string, where as the “Player” is a instance object. You can also use player names for debounces, so a killbrick would work for more than one player at the same time

local Players = game.Players
local Debounce = {}
Brick.Touched:Connect(function(Hit)
	local C = Hit.Parent
	local H = C:FindFirstChildOfClass("Humanoid")
	if H and H.Health > 0 then
		local Player = Players:GetPlayerFromCharacter(C)
		if Player and not Debounce[Player.Name] then
			Debounce[Player.Name] = true -- inserts the player name
			H:TakeDamage(10)
			task.wait(0.25)
			Debounce[Player.Name] = nil -- removes the player.name
		end
	end
end)

In output, it looks like it returns the same thing, but if you insert Player.Name in the table instead of Player, then you won’t be able to say

for _, Player in pairs(WinnersTable)
	Player.WinsStat += 1
end

But instead you should do

for _, PlayerName in pairs(WinnersTable)
	local Player = Players:FindFirstChild(PlayerName)
	if not Player then continue end
	Player.WinsStat += 1
end

btw i tried this and it works

local SurvivorsName = {}

for i, player in pairs(PLAYERS:GetPlayers()) do
			if player:FindFirstChild("Alive") then
				table.insert(survivors, player)
				table.insert(SurvivorsName, player.Name)
			end
		end
Status.Value = "Survivors: "..table.concat(SurvivorsName, ", ")

and this is the result :joy:
image

Is the issue that there is only 1 player in the table?

yes, maybe because i put it on a for i= 1, 3 do loop that’s why it keeps inserting the name of the player

if you’d mind here’s my full script also how would i know if the player is already in the table so i can stop inserting it

function Round.StartRound(timer)
	local outcome
	local survivors = {}
	local SurvivorsName = {}

	for i= timer, 0, -1 do

		for i, player in pairs(PLAYERS:GetPlayers()) do
			if player:FindFirstChild("Alive") then
				table.insert(survivors, player)
				table.insert(SurvivorsName, player.Name)
			end
		end

		Status.Value = toms(i)

		--if #survivors <= 1 or #survivors == 1 then
		--	outcome = "1-player-left"
		--	break
		--end

		if #survivors <= 0 or #survivors == 0 then
			outcome = "all-players-died"
			break
		end

		if i <= 0 or i == 0 then
			outcome = "time-up"
			break
		end

		task.wait(1)
	end

	--local Table = {"Charles", "Richard", "Leopold"}
	--local newString = "Survivors: "..table.concat(Table, ", ")
	--print(newString)

	if outcome == "all-players-died" then
		Status.Value = "No Survivors Left"
	elseif outcome == "time-up" then
		Status.Value = "Survivors: "..table.concat(SurvivorsName, ", ")
	elseif outcome == "1-player-left" then
		Status.Value = "Survivor: "..SurvivorsName[1].."."
	end

end

Each time you check for suvivors, you should reset the table.

	local survivors = {}
	local SurvivorsName = {}
	for i= timer, 0, -1 do
		survivors = {}
		SurvivorsName = {}
		for i, player in pairs(PLAYERS:GetPlayers()) do
			if player:FindFirstChild("Alive") then
				table.insert(survivors, player)
				table.insert(SurvivorsName, player.Name)
			end
		end
1 Like

Here’s the fully updated script. I fixed some minor stuff

function Round.StartRound(timer)
	local outcome
	local survivors = {}

	for i= timer, 0, -1 do
		survivors = {}
		for i, player in pairs(PLAYERS:GetPlayers()) do
			if player:FindFirstChild("Alive") then
				table.insert(survivors, player)
			end
		end
		Status.Value = toms(i)
		--if #survivors <= 1 then
		--	outcome = "1-player-left"
		--	break
		--end

		if #survivors == 0 then
			outcome = "all-players-died"
			break
		end

		if i == 0 then
			outcome = "time-up"
			break
		end

		task.wait(1)
	end

	if outcome == "all-players-died" then
		Status.Value = "No Survivors Left"
	elseif outcome == "time-up" then
		local WinnerNames = {}
		for i, player in pairs(survivors) do
			table.insert(WinnerNames,player.Name)
		end
		Status.Value = "Survivors: "..table.concat(WinnerNames, ", ")
	elseif outcome == "1-player-left" then
		Status.Value = "Survivor: "..SurvivorsName[1].."."
	end
end
1 Like