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.
If you want to make it so that it updates the labels automatically, you'll need to add a PlayerRemoving connect, and check if the player leaving is on the away team. If they are, then you re-run your code, so 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 end end end end -- sorry if these ends are not enough or are too much, I am doing this by eye. end end function check(plr) local team = game.Teams:FindFirstChild("away") if plr.TeamColor == team.TeamColor then bench() end end game.Players.PlayerRemoving:connect(check)
should work, haven't tested, It's 10:32 as I type this so it's defo sloppy