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
4 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 — 4y

2 answers

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

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

local PlayerList = {}

local function refresh()
    PlayerList = {}
    for _, plr in pairs(game.Players:GetChildren()) do --for every child (player) of the game.Players
        table.insert(PlayerList, plr.Name) --add the player's name to the list
    end
end

local function RandomPlayer()
    local num = math.random(1, #PlayerList) --get a random index number
    local player = PlayerList[num] --Get player with that index number
    for index, plr in pairs(PlayerList) do --Go through every player in the list
        if plr == player then --When the player is found...
            table.remove(PlayerList, index) --remove them from the list and...
            return game.Players[player] --return the player's instance
        end
    end
end

Here are the functions you need

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

To randomly pick a player, do:

local Players = game:GetService('Players')

local playerList = Players:GetPlayers()
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:

local Players = game:GetService('Players')

local previousPlayer

repeat
    local playerList = Players:GetPlayers()
    local randomPlayer = playerList[math.random(1, #playerList)]
until (previousPlayer ~= randomPlayer)

previousPlayer = randomPlayer

Answer this question