If you want to randomly choose a game object, you would use math.random()
How To Use Math.Random()
math.random() takes 2 arguments, the minimum value that it can go, and the maximum value it can go. So, it would look like this, math.random(1,10), this would choose a random number between 1 and 10. We still have to set it to a variable to use it.
local randomNum = math.random(1,10)
print(randomNum)
Result: killerbrenden - Since I would technically be the only one in the server.
If you want to randomize a list of objects, you should get the number of children the parent contains, and randomize that number and index the parent to get the random position in the list!
So, if you wanted to do it with players, you would do something like this.
local players = game:GetService("Players"):GetPlayers()
local randomPlayer = players[math.random(1,#players)]
print(randomPlayer.Name)
Result: killerbrenden - Since I would technically be the only one in the server.
Explanation
So, what we're doing here is getting the list of all the players in a table (Line 1). Next, we're indexing the players table, that contains all the players, and we're randomizing the number we're indexing by with math.random(1,#players), this will choose a number between 1 and the number of players there are in the game. Then, it will use that number, and index the players table with it, this will then choose a random player from the list and that is your player object. So, you can do randomPlayer.Name, randomPlayer.Character, etc.
Source
Hope this helped explain math.random!
1 comment
This site is a help site, NOT a request site.