If I run this script once, with 3 players on the team, it works fine. It comes out like this:
1 | Player 1
2 | Player 2
3 | Player 3
But, if player 2 leaves, and I ran the script again I'd want it to do this:
1 | Player 1
2 | Player 3
But instead, it does this:
1 | Player 1
3 | Player 3
The code is below. How can I fix this?
function bench()
local bench = game.StarterGui.InfoGui.Frame.ABenchP
for x, y in pairs(game.Teams:GetChildren()) do
if y:FindFirstChild("away") -- Indexes away team
then
for a, b in pairs(game.Players:GetChildren()) do
if b.TeamColor == y.TeamColor then
for i, v in pairs(game.Players:GetChildren()) do
if a == 1 then bench.a.Text = b.Name
v.PlayerGui.InfoGui.Frame.ABenchP.a.Text = b.Name
end
if a == 2 then bench.b.Text = b.Name
v.PlayerGui.InfoGui.Frame.ABenchP.b.Text = b.Name
end
if a == 3 then bench.c.Text = b.Name
v.PlayerGui.InfoGui.Frame.ABenchP.c.Text = b.Name
end
--Obviously I' m missing a few ends.. I chopped the code and I'm too lazy to add more.
I've updated my code || My goal: Make Player 1's name appear in the text label named "a", player 2's name to appear in the text label named "b", and player 3's name to appear in the text label named " c". It works, until a player is removed from the team. Their name stays in the text label, rather than player 3's name going into text label b and text label c becoming empty.

2 comments
A player's numerical index is going to change if the numerically ordered list of players has a previous index removed. A number that won't change is the player's UserID. It might help you more to explain what you are trying to do specifically to get a better recommendation for how to do it, rather than asking for help with your likely hackish approach.
I've updated my code || My goal: Make Player 1's name appear in the text label named "a", player 2's name to appear in the text label named "b", and player 3's name to appear in the text label named " c". It works, until a player is removed from the team. Their name stays in the text label, rather than player 3's name going into text label b and text label c becoming empty.