How do I get 2 random seperate players?

Hi, I’m trying to figure out how to get 2 random separate players

  1. What do you want to achieve? I’m trying to figure out how to get 2 random players that aren’t the same people.

  2. What is the issue? I cant seem to get 2 random players because there is always a chance of getting the same player with the script i’m using

  3. What solutions have you tried so far? I’ve scrolled through the dev forum but I haven’t found anything

-- what I'm using so far
local player = game.Teams.Lobby:GetPlayers()[math.random(1, #game.Teams.Lobby:GetPlayers())]
			local player2 = game.Teams.Lobby:GetPlayers()[math.random(1, #game.Teams.Lobby:GetPlayers())]
--if ur a dev ur probably cringing at this attempt, sorry

The only thing you need to do is check if player 2 is player 1, just add this on the bottom of the script:

if player2 == player then
   repeat player2 = game.Teams.Lobby:GetPlayers()[math.random(1, #game.Teams.Lobby:GetPlayers())]
   until player2 ~= player

Damnnnn, thanks a lot. Can’t believe I didn’t think of that.

1 Like

An alternative solution that avoids polling:

local plrTable = game.Teams.Lobby:GetPlayers()
local player = game.Teams.Lobby:GetPlayers()[math.random(1, #game.Teams.Lobby:GetPlayers())]

table.remove(plrTable, table.find(plrTable, player))
local player2 = plrTable[math.random(1, #plrTable)]
1 Like