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

How do you assign 4 even teams?

Asked by 9 years ago

I have a script that gets 4 teams from Lighting, clones them, puts them in the Teams service, and then (is supposed to) assigns players to it. The problem is, it won't assign players to the teams. This is the script for cloning the teams/map, and moving them to the right place:

            map = game.Lighting.Crossroads:Clone()
            red = game.Lighting.Red:Clone()
            blue = game.Lighting.Blue:Clone()
            green = game.Lighting.Green:Clone()
            yellow = game.Lighting.Yellow:Clone()
            map.Parent = game.Workspace
            red.Parent = game.Teams
            blue.Parent = game.Teams
            green.Parent = game.Teams
            yellow.Parent = game.Teams

That all works fine and dandy, but when I try to assign players to the teams, with:

player = game.Players:GetChildren()
for p = #player, #player do
                if(player[p].TeamColor==BrickColor.new("White"))and (player[p].Character~=nil)then
                    player[p].TeamColor = blue.TeamColor
                end
            end
            for p = #player/2, #player do
                if(player[p].TeamColor==blue.TeamColor)and (player[p].Character~=nil)then
                    player[p].TeamColor = red.TeamColor
                end
            end
            for p = #player/2, #player do
                if(player[p].TeamColor==red.TeamColor)and (player[p].Character~=nil)then
                    player[p].TeamColor = green.TeamColor
                end
            end
            for p = #player/2, #player do
                if(player[p].TeamColor==blue.TeamColor)and (player[p].Character~=nil)then
                    player[p].TeamColor = yellow.TeamColor
                end
            end

It doesn't work. I looked in output, and nothing shows up. Please help :)

0
this is a fm script, but you want to divide the #players by 4 not 2... HungryJaffer 1246 — 9y
0
I am dividing by 2 because I want to keep people on blue: move all 2 blue, take half of blue to red, take half of red to green, and half of blue to yellow. That would put it into quarters. UnityAlex 36 — 9y
0
I need help :( UnityAlex 36 — 9y

2 answers

Log in to vote
1
Answered by 9 years ago
local players = {}
for a,b in pairs(game.Players:GetChildren()) do
table.insert(players,b)
end

for i = 1,#players do
if i % 1 == 0 then
players[i].TeamColor = BrickColor.new("Bright red") -- change to the color you want team 1
elseif i % 2 == 0 then
players[i].TeamColor = BrickColor.new("Bright blue") -- again change to the color you want
elseif i % 3 == 0 then
players[i].TeamColor = BrickColor.new("Bright yellow") -- again
elseif i % 4 == 0 then
players[i].TeamColor = BrickColor.new("Bright green") -- again
end
end

Should work. I scripted it in here so I never tested it (my studio's down atm and it's working).

0
Thanks for the input, though I tested it in Studio and it didn't work. Output doesnt say anything. UnityAlex 36 — 9y
0
The trouble is if i%4 is 0, i%2 would also be 0. Plus i%1 is always 0 so it'll always do the first one. The way to fix that is to reverse the order of them. GoldenPhysics 474 — 9y
0
I suppose that would be the problem, but it isn't even assigning me to a team. UnityAlex 36 — 9y
0
It doesn't help to reverse the order UnityAlex 36 — 9y
0
I don't know then. Sorry ObscureEntity 294 — 9y
Ad
Log in to vote
1
Answered by 9 years ago

Here is the works: (Extremely inefficient, but HEY, IT WORKS! :D)

local Teams ={green, yellow, red, blue}
function AssignRandomTeam(Player, CurrentTeam)
    for x=1,#Teams do
        if Teams[x] ~= CurrentTeam then
        Player.Parent = Teams[x]
    end
end
function Relocate(Team)
    for i, v in pairs(Team:GetChildren()) do
        AssignRandomTeam(v, Team)
        return 
    end
end
while wait() do
    local NumBlue   = blue:children()
    local NumRed    = red:children()
    local NumYell   =yellow:children()
    local NumGrn    =green:children()
    local TmNums    = {NumGrn, NumYell, NumBlue, NumRed}

    for i = 1, #TmNums do
        if #TmNums[i] > #TmNums/game.Players.NumOfPlayers then
            Relocate(Teams[i])
        end
    end
end
0
PS I'm sorry if I screwed up, I am rusty on Teams and such deputychicken 226 — 9y

Answer this question