So I want to make a script that makes the game pick 1+ player randomly and is not the same person the game chose before.
Make a list of all the players and randomly pick. After picking a player, remove them from the list
01 | local PlayerList = { } |
02 |
03 | local function refresh() |
04 | PlayerList = { } |
05 | for _, plr in pairs (game.Players:GetChildren()) do --for every child (player) of the game.Players |
06 | table.insert(PlayerList, plr.Name) --add the player's name to the list |
07 | end |
08 | end |
09 |
10 | local function RandomPlayer() |
11 | local num = math.random( 1 , #PlayerList) --get a random index number |
12 | local player = PlayerList [ num ] --Get player with that index number |
13 | for index, plr in pairs (PlayerList) do --Go through every player in the list |
14 | if plr = = player then --When the player is found... |
15 | table.remove(PlayerList, index) --remove them from the list and... |
16 | return game.Players [ player ] --return the player's instance |
17 | end |
18 | end |
19 | end |
Here are the functions you need
To randomly pick a player, do:
1 | local Players = game:GetService( 'Players' ) |
2 |
3 | local playerList = Players:GetPlayers() |
4 | local randomPlayer = playerList [ math.random( 1 , #playerList) ] |
To make it not pick the previous player, store the previous player as a variable and compare it with the new player:
01 | local Players = game:GetService( 'Players' ) |
02 |
03 | local previousPlayer |
04 |
05 | repeat |
06 | local playerList = Players:GetPlayers() |
07 | local randomPlayer = playerList [ math.random( 1 , #playerList) ] |
08 | until (previousPlayer ~ = randomPlayer) |
09 |
10 | previousPlayer = randomPlayer |