I need to make a function that will put 4 people on the Bright red team, 2 on the Bright blue team, and the rest on the Bright green team. How would I go about selecting these players randomly and putting them on these teams? I have no idea how to do this, so I will not supply any code as I know it wont help with the process.
local brightRed = BrickColor.new("Bright red") local brightBlue = BrickColor.new("Bright blue") local brightGreen = BrickColor.new("Bright green") if game.Players.NumPlayers > 6 then for i = 1, #game.Players:GetPlayers() do if i <= 4 then game.Players[game.Players:GetPlayers()[i]].TeamColor = brightRed elseif i > 4 and i <=6 then game.Players[game.Players:GetPlayers()[i]].TeamColor = brightBlue else game.Players[game.Players:GetPlayers()[i]].TeamColor = brightGreen end end end
local minimum_plrs = 6 local red = BrickColor.new("Really red") local blue = BrickColor.new("Bright blue") local green = BrickColor.new("Bright green") if game.Players.NumPlayers >= minimum_plrs then local already_chosen = {} local color local players = game.Players:GetPlayers() for i = 1, #players do local randy color = (i > 6) and green or (i <= 2) and blue or red repeat randy = players[math.random(1, #players)] wait() until not already_chosen[randy.Name] already_chosen[randy.Name] = true randy.TeamColor = color or BrickColor.White() end end
ternary operator's make things shorter and are extremely useful. They're kind of difficult to understand, but this is basically what I'm saying;
color = (i > 6) and green or (i <= 2) and blue or red
if i > 6 then -- more than 6 color = green elseif i <= 2 then -- less than or = 2 color = blue else color = red -- more than 2, less than 6. end
-- Due to concerns in the comments, you can use this to test it. local minimum_plrs = 6 local red = BrickColor.new("Really red") local blue = BrickColor.new("Bright blue") local green = BrickColor.new("Bright green") local test = Workspace.Model --if game.Players.NumPlayers >= minimum_plrs then local already_chosen = {} local color local players = test:GetChildren() for i = 1, #players do local randy color = (i > 6) and green or (i <= 2) and blue or red repeat randy = players[math.random(1, #players)] wait() until not already_chosen[randy.Name] already_chosen[randy.Name] = true randy.BrickColor = color or BrickColor.White() end --end