Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Choosing Two Random Players From A Team?

Asked by 5 years ago

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

Log in to vote
1
Answered by 5 years ago
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.

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? LukeGabrieI 73 — 5y
Ad

Answer this question