Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

[Ez] Help with in pairs?

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

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.

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.

1 answer

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

2 comments

0

Playerremoving as in leaving the game? Or leaving the team?

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

This discussion is preserved as it was. New answers, comments, and votes are closed.