Sorry, but I'm in the process of creating a game. But, there's a problem: I have no idea how to choose 2 random players.
Any help?
By using math.random of course!
while(true) do repeat wait() until game.Players.NumPlayers >=2 local Players=Game.Players:GetPlayers() local Chosen1, Chosen2 = math.random(1, #Players), 0 repeat Chosen2 = math.random(1, #Players) until Chosen2 ~= Chosen1 print(Players[Chosen1].Name) print(Players[Chosen2].Name) end
Try :
local player = math.random(2, game.Players.NumPlayers); local Players = game.Players:GetPlayers() -- SCRIPT HERE -- -- Use player to actually apply to the two players. -- Example : player.Character:BreakJoints
Let's break it down.
To get a list of players, call game.Players:GetPlayers(). This returns a table of all the players in the game.
The table might look like this:
{ [1] = game.Players.Merely; [2] = game.Players.MrSmenryBackup; [3] = game.Players.Test; }
The first number you see is the index, which tells you what position in the table that element is at. For example, MrSmenryBackup's index is 2.
To select a random player from the table, you can use math.random. It gives you a random number between 1 and a number you pass in.
local players = game.Players:GetPlayers() local randomPlayer = players[math.random(#players)]
If you put the pound sign (#) in front of a table, it gives you the number of entries in that table. So that code picks a random number between 1 and the number of players, and picks out the player from the table with that index.