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

How can I choose two random players that are not the same?

I'm trying to make a game that will teleport two players into an arena to fight. I've already made the script although whenever I run it sometimes it would only teleport one player. I want to grab two players that are not the same. This is my attempt

local NumbPlayers = #game.Players:GetPlayers()
	if NumbPlayers >= 2 then
                local randomPlayer1 = game.Players:GetPlayers() 
        [math.random(1,#game.Players:GetPlayers())]
                local randomPlayer2 = game.Players:GetPlayers()[math.random(1,#game.Players:GetPlayers())]
                print(randomPlayer1)
                print(randomPlayer2)
                if randomPlayer2 == randomPlayer1 then
	          randomPlayer2 = game.Players:GetPlayers()[math.random(1,#game.Players:GetPlayers())]
               end
end

I do have a bit of a precaution that if randomplayer2 is equal to randomplayer1 then it will choose a new player. Although sometimes that doesn't even work. Any help would be apreciated

Regards, Bl_ueHistory

3 answers

✓ Accepted answer

Instead of using if they are equal then change, try using while they are equal then change randomplayer2

So first, let's get the name of every player in a table:

local Players = {}

for _,v in pairs(game.Players:GetChildren()) do
	table.insert(holdPlayers, v.Name)
end

Now, let's choose the 1st player:

local Players = {}

for _,v in pairs(game.Players:GetPlayers()) do
	table.insert(holdPlayers, v.Name)
end

local 1stRandomNumber = math.random(1,#game.Players:GetChildren())
local 1stRandomPlayerName = Players[randomNum]
local 1stPlayer = game.Players:WaitForChild(1stRandomPlayerName)

Okay. Now, let's get the 2nd player:

local Players = {}

for _,v in pairs(game.Players:GetPlayers()) do
	table.insert(holdPlayers, v.Name)
end

local 1stRandomNumber = math.random(1,#game.Players:GetChildren())
local 1stRandomPlayerName = Players[randomNum]
local 1stPlayer = game.Players:WaitForChild(1stRandomPlayerName)

local 2ndRandomNumber = math.random(1,#game.Players:GetChildren())
local 2ndRandomPlayerName = Players[randomNum]
local 2ndPlayer = game.Players:WaitForChild(2stRandomPlayerName)

But we don't want that the 1st player the is the same as the 2nd player, so let's do a "repeat" function that randomizes again the 2nd player until we get another player that is not the 1st player:

local Players = {}
for _,v in pairs(game.Players:GetPlayers()) do
	table.insert(Players, v.Name)
end

local 1stRandomNumber = math.random(1,#game.Players:GetChildren())
local 1stRandomPlayerName = Players[randomNum]
local 1stPlayer = game.Players:WaitForChild(1stRandomPlayerName)

local 2ndPlayerIsNot1stPlayer = false -- variable for the repeating

repeat
	local 2ndRandomNumber = math.random(1,#game.Players:GetChildren())
	local 2ndRandomPlayerName = Players[randomNum]

	if 2ndRandomPlayerName = 1stRandomPlayerName then -- checking their names
		2ndPlayerIsNot1stPlayer = false
  	else
		local 2ndPlayer = game.Players:WaitForChild(2stRandomPlayerName) -- getting the 2ndplayer after making sure it's not the 1st player
		2ndPlayerIsNot1stPlayer = true
	end
until 2ndPlayerIsNot1stPlayer == true
-- Add what you wanna do here

It's a little bit long, but I hope it helped!

I haven't tested it, but this is probably the method I would go with. It's easy to understand and should work.

local Players = game.Players:GetPlayers()

local function ChooseRandom()
	local newRandom = math.random(1, #Players) --//Gets a random index inside of the Players table
	local newPlayer = Players[newRandom] --//Gets the player object from the Players table
	
	table.remove(Players, newRandom) --//Removes the player object that we just chose from the table
	
	return newPlayer --//Return our new random player
end

local FirstPlayer = ChooseRandom()
local SecondPlayer = ChooseRandom()

1 comment

0

Note I would probably also put all of this in a function and just return the First and Second player chosen, this way the Players table will get updated with current players in the server.

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