Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

[Answered] How to select 2 random players?

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?

1 comment

0

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

1 answer

✓ Accepted answer
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 = Random.new(tick()) -- creates a new Random.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
end

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()
        wait()
    end

    -- Now you have 2 vars, Player1 and Player2

	print(Player1, Player2)
else
	print('Insufficient players')
end

1 comment

0

Thank you

This discussion is preserved as it was. New answers, comments, and votes are closed.