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

I have a script that loops through players, but does not work why?

Asked by 7 years ago

I have a script that loops through players to change their teams, but does not work why? Here is the script:

local players = game.Players.NumPlayers
for i = 1, players do
    p.Team = game.Teams.Eliminating
print('All changed to eliminating')
            end 

WHere is the error, and what can I replace it with?

1 answer

Log in to vote
1
Answered by 7 years ago
Edited 7 years ago

Your loop doesn't loop throught the players. It loops the amount of players.

To get all the players we use GetPlayers() function.

so

local players = game.Players:GetPlayers() -- the table of players

You didn't set any variable for "p"

Also to change player's team you would use TeamColor

Once we've created a table we simply need to put "#" to get the number or variables in the table.

The final script

local players = game.Players:GetPlayers() -- Creating the table 
for i = 1,#players do --- Getting the table's length
    players[i].TeamColor = game.Teams.Eliminating.BrickColor -- Changing player's team
print('All changed to eliminating')
            end 
Ad

Answer this question