Still have questions? Join our Discord server and get real time help.
Log in to vote

[Answered] How to select 2 random players? [closed]

Asked by
Despayr 151
9 months ago
Edited 9 months ago

How would I go about a script that chooses a random player, then after a few seconds it chooses another without selecting the same player?

instead of putting [answered] in the title, click the "Accept answer" button below AyeeAndrxw's answer. RubenKan 3216 — 9mo

Closed as Not Constructive by RubenKan

This question has been closed because it is not constructive to others or the asker. Most commonly, questions that are requests with no attempt from the asker to solve their problem will fall into this category.

Why was this question closed?

1 answer

Log in to vote
Answered by 9 months ago
local Players = game:GetService('Players')

local function ChooseRandomPlayer() -- function to choose a player
    local Player = nil
    local CurrPlayers = Players:GetPlayers() -- gets all the current players

    local R = -- creates a new along with tick() for more randomness
    local Num = R:NextInteger(1, #CurrPlayers) -- returns a number between 1 and the current player count
    Player = CurrPlayers[Num]

    return Player

if not (#Players:GetPlayers() < 2) then
    local Player1 = ChooseRandomPlayer()

    wait(5) -- Change to your amount of seconds

    local Player2 = Player1

    while Player2 == Player1 do -- Changes player2 to a new player until player2 isn't player1
        Player2 = ChooseRandomPlayer()

    -- Now you have 2 vars, Player1 and Player2

    print(Player1, Player2)
    print('Insufficient players')
Thank you Despayr 151 — 9mo