If I run this script once, with 3 players on the team, it works fine. It comes out like this:
1 | 1 | Player 1 |
2 | 2 | Player 2 |
3 | 3 | Player 3 |
But, if player 2 leaves, and I ran the script again I'd want it to do this:
1 | 1 | Player 1 |
2 | 2 | Player 3 |
But instead, it does this:
1 | 1 | Player 1 |
2 | 3 | Player 3 |
The code is below. How can I fix this?
01 | function bench() |
02 | local bench = game.StarterGui.InfoGui.Frame.ABenchP |
03 |
04 | for x, y in pairs (game.Teams:GetChildren()) do |
05 | if y:FindFirstChild( "away" ) -- Indexes away team |
06 | then |
07 | for a, b in pairs (game.Players:GetChildren()) do |
08 | if b.TeamColor = = y.TeamColor then |
09 | for i, v in pairs (game.Players:GetChildren()) do |
10 | if a = = 1 then bench.a.Text = b.Name |
11 | v.PlayerGui.InfoGui.Frame.ABenchP.a.Text = b.Name |
12 | end |
13 | if a = = 2 then bench.b.Text = b.Name |
14 | v.PlayerGui.InfoGui.Frame.ABenchP.b.Text = b.Name |
15 | end |
16 | if a = = 3 then bench.c.Text = b.Name |
17 | v.PlayerGui.InfoGui.Frame.ABenchP.c.Text = b.Name |
18 | end |
19 |
20 | --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:
01 | function bench() |
02 | local bench = game.StarterGui.InfoGui.Frame.ABenchP |
03 |
04 | for x, y in pairs (game.Teams:GetChildren()) do |
05 | if y:FindFirstChild( "away" ) -- Indexes away team |
06 | then |
07 | for a, b in pairs (game.Players:GetChildren()) do |
08 | if b.TeamColor = = y.TeamColor then |
09 | for i, v in pairs (game.Players:GetChildren()) do |
10 | if a = = 1 then bench.a.Text = b.Name |
11 | v.PlayerGui.InfoGui.Frame.ABenchP.a.Text = b.Name |
12 | end |
13 | if a = = 2 then bench.b.Text = b.Name |
14 | v.PlayerGui.InfoGui.Frame.ABenchP.b.Text = b.Name |
15 | end |
should work, haven't tested, It's 10:32 as I type this so it's defo sloppy