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

How would I use math.random to pick two people from each team?

Purpose is to get one person from the Red Team and the Blue Team. It picks the people, but sometimes it'll pick two people on the same team, leaving the other team without a captain.

function PickCaptains()
	local Players = game.Players:GetPlayers()
	for i, v in pairs(Players) do
		if v.TeamColor == RedTeam.TeamColor then
		local RedLeader = Players[math.random(1,#Players)]
		end
		if v.TeamColor == BlueTeam.TeamColor then
		local BlueLeader = Players[math.random(1,#Players)]
		end
	end
end

1 answer

✓ Accepted answer

The problem is that you're picking from all the possible Players instead of picking from the possible team of Players.

local redteam = {}
local blueteam = {}
local redcapt
local bluecapt
local Plyrs = game.Players:GetPlayers()

function SeparateTeams() -- First you need to separate both teams.
for i,v in pairs(Plyrs) do
if v.TeamColor == RedTeam.TeamColor then
table.insert(redteam,#redteam+1,v) -- We'll place all players in the RedTeam into the redteam table.
elseif v.TeamColor == BlueTeam.TeamColor then
table.insert(blueteam,#blueteam+1,v) -- We'll also add the players in the BlueTeam to the blueteam table.
end
end

function PickCaptains() -- Now we can pick a captain for both teams
local redcapt = redteam[math.random(1,#redteam)]
local bluecapt = blueteam[math.random(1,#blueteam)]
end

And there you go, not sure if this will work w/ your game since I haven't really done anything w/ teams or really scripted in a while but if you have any other questions feel free to ask. Hope this helps.

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