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

Choosing Two Random Players From A Team?

Basically I have a team where everybody starts as, and 2 players that are randomly selected from this team will play a game and this'll happen until there's nobody in the team that everybody started at.

local teams = game:GetService("Teams")
local matchmaking = teams:WaitForChild("Matchmaking")

local round1 = matchmaking:WaitForChild("Round 1")
local round2 = matchmaking:WaitForChild("Round 2")
local round3 = matchmaking:WaitForChild("Round 3")

wait(10)

for i, v in pairs(game.Players:GetPlayers()) do
	if v.Team == game.Teams["Not Playing"] then
		local noteam = game.Teams["Not Playing"]
		local chooseplayers = noteam:GetPlayers() -- I don't know if this is correct
		local Chosen1, Chosen2 = math.random(1, #chooseplayers)
		repeat Chosen2 = math.random(1, #chooseplayers) until Chosen2 ~= Chosen1
		print(chooseplayers[Chosen1].Name)
		print(chooseplayers[Chosen2].Name)
	end
end

Thanks,

LukeGabrieI

1 answer

✓ Accepted answer
local team = game:GetService("Teams"):FindFirstChild("Not Playing")

local playersInTeam = team:GetPlayers()

if #playersInTeam > 0 then -- make sure there are players in this team
    local player1 = playersInTeam[math.random(1, #playersInTeam)
    local player2 = playersInTeam[math.random(1, #playersInTeam)
    
    if player1 ~= player2 then
        print(player1.Name)
        print(player2.Name)
    end
end

Might wanna wrap this in a while/repeat loop, or an event so it runs multiple times.

1 comment

0

I was wondering how would I do it if I placed Players' Names into a Table, and tried selecting two random players inside a table? How would I go upon doing that?

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