Random Player Code not working

Please Help Me Fix this Code

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

I want to Get it to choose a random Player then print the players name

There are no prints in the output bar

LargeHotDogs13

If you’re running this right when the game starts, the player may not have joined when the server gets the number of players in the game. Try waiting for a player to join like this:

local Players = game.Players

if #Players:GetPlayers() < 1 then
	Players.PlayerAdded:Wait()
end

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

math.random should always have 2 number

math.random(1,10)

local Players = game.Players
local RandomPlayer = Players:GetPlayers()[math.random(1,#Players:GetPlayers()]
print (RandomPlayer)

1 Like

It doesn’t need to though. I thought the same thing awhile ago and it even seemed weird to only do it with one parameter, but you can. If you just pass in a single number you’ll get a random number from 1 to that number.

for i = 1, 5 do
     print(math.random(10))
end
1 Like

Wait, really? I thought that you must have 2 numbers.