Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I make the script choose a random player?

Asked by 4 years ago
Edited 4 years ago

Like i am trying to make a camping game(by samsonxvi) so i need to make it choose a random player's https://www.roblox.com/Thumbs/Avatar.ashx?x=100&y=100&username= different players also i want it to choose a random players name. SOMEBODY PLS HELP ME!!!

1 answer

Log in to vote
5
Answered by
royaltoe 5144 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

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.

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(). DeceptiveCaster 3761 — 4y
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. theking48989987 2147 — 4y
0
Thanks! DahNOOB_TY 4 — 4y
Ad

Answer this question