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

Choosing Random Player, Help!?

Tried making a random player chooser but doesn't work.. please help me fix this, or you could write a new script explaining the actions

players = game.Players:GetChildren()

randoms = players[math.random(1,#players)]
chosen = randoms.Name
print(chosen)

Please help :(

1 comment

0

Try running it only when you know you have multiple players.

3 answers

I just tested out this script, and now I know whats wrong. Your script is running when there is no players on the game. Here is a script that will give you the random player, after the player has joined.

game.Players.ChildAdded:connect(function()
	players = game.Players:GetChildren() --Gets a table of all the players
	randomPlayer = players[math.random(1, #players)] --Gets a random player from the players table
	print(randomPlayer.Name) --Prints the name of the random player
end)

I hope this helps, and if you have any questions, just comment.

-- You can take out the while wait() do loop and still have my script run correctly. :D Hope this helps you!

while wait() do



local plr=game.Players:GetPlayers()[math.random(1,game.Players.NumPlayers)] 
print(plr.Name)



wait(10)
end

It only works in solo though, doesn't work in - game :(

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