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

How do I make the game choose more than 1 random player?

Asked by
Ieowyyn 69
5 years ago

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.

1
good luck greatneil80 2647 — 5y

2 answers

Log in to vote
1
Answered by
sheepposu 561 Moderation Voter
5 years ago
Edited 5 years ago

Make a list of all the players and randomly pick. After picking a player, remove them from the list

01local PlayerList = {}
02 
03local 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
08end
09 
10local 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
19end

Here are the functions you need

0
Well thanks but where do I put the script that the random players will do? Ieowyyn 69 — 5y
0
It depends on what you are doing. Usually for this stuff, you put a script in the ServerScriptService sheepposu 561 — 5y
Ad
Log in to vote
0
Answered by 5 years ago

To randomly pick a player, do:

1local Players = game:GetService('Players')
2 
3local playerList = Players:GetPlayers()
4local 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:

01local Players = game:GetService('Players')
02 
03local previousPlayer
04 
05repeat
06    local playerList = Players:GetPlayers()
07    local randomPlayer = playerList[math.random(1, #playerList)]
08until (previousPlayer ~= randomPlayer)
09 
10previousPlayer = randomPlayer

Answer this question