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

How do I make the script choose a random player?

1 answer

✓ Accepted answer

You can use :GetPlayers() to get a table of players and then use math.random(smallestNumber, biggestNumber) to pick a random player in that table.

# means number of items. if you say print(#players) you are printing out the number of players.

here's what the code would look like:

players = game.Players:GetPlayers()
randomPlayer = players[math.random(1,#players)]

keep in mind, if you try running this in a script immediately like this, there won't be any players and you'll get the error: bad argument #2 to 'random' (interval is empty) so make sure you're using the code above when there's actual players in game.

3 comments

0

^ This, but it's pseudorandom. If you want completely random results, create a random seed of tick() using math.randomseed() before using math.random().

0

false, both are psuedo-random , roblox uses an internal entropy source when you dont provide a seed. A psuedorandom number generation algorithm generally uses a seed, performs some artihmetic on that number, then uses the modulus operator to give the appearance of randomness. In this case, its either you give it a seed or roblox gives it a seed, not one being completely random, and the other not.

0

Thanks!

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