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

Help with getting players from a table?

EDITED WITH WHAT I NOW HAVE

local players = game.Players:GetPlayers()
local blue = {}
local red = {}
	
for number = 1, #players do
	local setPlayer = players[number]
	if setPlayer.TeamColor == BrickColor.new("Cyan") then
		table.insert(blue, players)
	elseif setPlayer.TeamColor == BrickColor.new("Persimmon") then
		table.insert(red, players)
	end
end
	
for number = 1, #blue do
	local setPlayer = blue[number]
	local frames = {}
	for i = 1, 9 do
		frames[i] = blueBox.PlayerBox:WaitForChild("Player" .. i)
	end
	if setPlayer then
		frames[number].NameLabel.Text = setPlayer.Name
	end
end
	
for number = 1, #red do
	local setPlayer = red[number]
	local frames = {}
	for i = 1, 9 do
		frames[i] = redBox.PlayerBox:WaitForChild("Player" .. i)
	end
 	if setPlayer then
		frames[number].NameLabel.Text = setPlayer.Name
	end
end

This script basically gets all the players in the game, and sorts them into 2 tables, red and blue

Then I have 2 for loops, which go through the tables, and get the players in the table, and sets a TextLabel.Text to equal the name of the player.

The problem is that instead of having 1 frame saying Player1 and the other Player2 it has both as the LocalPlayer, so on Player1's screen, it has both frames on the blue and red side saying Player1, and vice versa for Player2

1 answer

✓ Accepted answer

You setting the variable setPlayer to the same thing in both examples, players[1]. You want to go though the players in the red and blue tables, rather than the table of all players:

for n = 1, #blue do
	local setPlayer = blue[n]
	...
end

for n = 1, #red do
	local setPlayer = red[n]
	...
end

1 comment

0

it has an error on line 32 of my code, saying 'bad argument #3 to Text (string expected, got nil)'

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