Need Help getting Random Player

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.

1 Like

Ok, after that how should I access a random player? Or would I just do this:

local RandomPlayer = math.Random(#game.Players:GetPlayers())
RandomPlayer.Character.UpperTorso.Transparency = 1

Would that make a random players upper torso transparent?

1 Like

Assume you set Players service to the variable with the same name:

local RandomPlayer = Players:GetPlayers()[math.random(#Players:GetPlayers())]
RandomPlayer.Character.UpperTorso.Transparency = 1

This will work, unless the circumstance is that the player hasn’t loaded their character yet.


Ok thank you! I will try this out right now.

1 Like

It say this is the output:
Attempted To call a nil value, it is saying line 11 and on that line it does not need my character yet.

You haven’t declared the Players variable.

local Players = game:GetService("Players")

1 Like

I declared the playerService variable and put that there. This is what I have put down:

playerService = game:GetService("Players")

local RandomPlayer = playerService[math.random(#Players:GetPlayers())]
RandomPlayer.Character.UpperTorso.Transparency = 1

It still gives me the error in the output.

You aren’t getting the table from the service correctly, you’re trying to index something that doesn’t exist on the service. Replace it with:

local RandomPlayer = playerService:GetService()[math.random(#Players:GetPlayers())]
1 Like

I just put that down and it gave me a new error, this is what the error said:
16:49:16.767 - GetService is not a valid member of Players “Players”

Woops. I’m tired today. I meant GetPlayers.


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. :frowning:

Could you describe and elaborate the error?

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(,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.

I added in the player added and character added events and it is still giving me this error:
17:35:58.407 - Workspace.ImportantScripts.MainGameLoop:10: invalid argument #1 to ‘random’ (interval is empty)

Here is my script if this helps:

local RandomPlayer = playerService:GetPlayers()[math.random(#Players)]

print("Player Added")
		print("Character Added")
		if RandomPlayer.Team == game.Teams.Audience then

			print("Test Success")
			local Judge1 = RandomPlayer
			Judge1.Team = game.Teams.Judges
			Judge1.Character:MoveTo(, 258.95, 233.305))
			local Judge2 = RandomPlayer
			local Judge3 = RandomPlayer


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.

I am sorry, what do you mean by re-roll. Shouldn’t this only be going through players in the game?

Basically by setting the variable anew again:

RandomPlayer = playerService:GetPlayers()[math.random(#Players)]

Ok I put that in the characterAdded event, let me see if this works, thanks!