I am trying to get a random player to change that random players team. I already know how to change a players team but I just cannot figure out how to get a random player. Here is the script I am using that does not work:
local RandomPlayer = Players[math.random(game.Players:GetPlayers())]
Add # operator in front of this, because the function returns a table. You want the length of the table, or in other words, the quantity of players in the server. This operator returns a number based on the length of the table.
Whenever more then one person joins the game it gives me an error, but when it is only me it runs fine. For some reason my computers copy and paste is just not working right now.
The error is saying attempted to index nil with MoveTo. I am trying to move a random player to an area and it works when one person is in the game but as soon as two or more people join it errors. I put this:
RandomPlayer.Character:MoveTo(Vector3.new(X,Y,Z) but the X Y and Z were actual coordinates.
It is likely that the Character hasn’t loaded in yet. Try a different approach of populating a table with players who have triggered the CharacterAdded event and randomize any player from that table.
That’s not exactly what I implied on with the other table. Alternatively, try to do an if statement for the RandomPlayer.Character. If it doesn’t exist, re-roll until the player is right.