Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

[Ez] Help with in pairs?

Asked by 8 years ago

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.

1
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. 1waffle1 2908 — 8y
0
**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. Ethan_Waike 156 — 8y

1 answer

Log in to vote
1
Answered by
shayner32 478 Trusted Moderation Voter
8 years ago

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

0
Playerremoving as in leaving the game? Or leaving the team? Ethan_Waike 156 — 8y
0
PlayerRemoving is an event that fires when a player is disconnecting from the game - check the wiki link that I linked. Basically, if a player is leaving, they are also leaving their team - which means, if a player that is on the away team LEAVES, they are gone and shouldn't be on your textlabels. So, when a player LEAVES the game, the script will check if that player was on the away team, and if shayner32 478 — 8y
Ad

Answer this question