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

How do I randomize teams in Roblox?

Asked by 8 years ago

Yeah, I've never been good with randomizing, but this is what I've got but it doesn't work?

local function SplitPlayers()
local players = {}
for _, player in pairs(game.Players:GetPlayers()) do
if player.TeamColor == BrickColor.new("Medium stone grey") then
table.insert(players, player)
end
end

local redTable = {}
local blueTable = {}
local redPlayers = math.floor(1/2 + #players * 4/6)
local bluePlayers = #players - redPlayers

for i = 1, redPlayers do
local player = table.remove(players, math.random(#players))
table.insert(redTable, player)
player.TeamColor = BrickColor.new("Really red") 
end

for i = 1, bluePlayers do
local player = table.remove(players, math.random(#players))
table.insert(blueTable, player)
player.TeamColor = BrickColor.new("Bright blue")
end
end

3 answers

Log in to vote
1
Answered by 8 years ago
local teams = {"Bright blue","Bright red"}

local number = 1

    for _, player in pairs(game.Players:GetChildren()) do
                player.TeamColor = BrickColor.new(teams[number])
                number = number + 1
                if number > #teams then
            number = 1
           end
    end

Change what's inside the teams table to the team color you want, just make sure they are BrickColor's.

0
Please explain your answer. Simply code is not enough. ChemicalHex 979 — 8y
0
Wow downvote me for giving him the answer e.e NinjoOnline 1146 — 8y
0
Yes it's an answer but this will not help him learn unless he understands what this does and how it works, and most likely wont as he didn't make it like this in the first place. Explain code, don't just post it and say "Here you go" Uroxus 350 — 8y
0
Yes, I agree with ChemicalHex and MrLegoman900, you need to explain your answer. Even I couldn't understand it at first. And make sure you format your code properly, it's a bit wonky. TheDeadlyPanther 2460 — 8y
Ad
Log in to vote
0
Answered by 6 years ago
Edited 6 years ago
wait(10) -- Change 10 to the amount of time you want this to happen
for i, v in pairs(game.Teams["White Team"]:GetPlayers())do
v.Character.Head:Destroy()
end

Change White Team to the team were you will get all the players. Might not be a team randomizer but it works.

0
Make it a script, Put it in ServerScriptService SkatePro888899999 5 — 6y
0
i did it but it said this in output: ServerScriptService.Script:2: bad argument #1 to '(for generator)' (table expected, got nil) AnotherPerson_999 28 — 5y
Log in to vote
0
Answered by 4 years ago

I have the perfect idea! I write script only in ServerScriptService:

local teams = {'Really red', 'Really blue'}
local team = {{''},{''}} -- Red and Blue team

game.Players.PlayerAdded:Connect(function(plr) -- Add player in random team
    local plrs = game.Players:GetChildren()
    if #team[1] == #team[2] then
        local choosed = math.random(1,2)
        table.insert(team, choosed, plr)
        plr.TeamColor = BrickColor.new(teams[choosed])
    elseif #team[1] < #team[2] then
        table.insert(team, 1, plr)
        plr.TeamColor = BrickColor.new(teams[1])
    elseif #team[1] > #team[2] then
        table.insert(team, 2, plr)
        plr.TeamColor = BrickColor.new(teams[2])
    end
end)

game.Players.PlayerRemoving:Connect(function(plr) -- Remove player team
    if plr.TeamColor == 'Really red' then
        for i = 2, #team[1] do
            if team[1][i] == plr.Name then
                table.remove(team, 1, i)
                break
            end
        end
    else
        for i = 2, #team[2] do
            if team[2][i] == plr.Name then
                table.remove(team, 2, i)
                break
            end
        end
    end
end)

Answer this question