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

Help with sorting teams?

Asked by
KordGamer 155
8 years ago

I'm making a new game and I need to sort people into teams. I tried to do it already but it didn't work... Could I have some help with how I can possibly make this happen? This is the script :

--Made by KordGamer
--Varibales--
Text = game.Lighting.Text
Maps = {"Testing", "Testing 2"}
NumMaps = 2
--------------

--------Script--------
---game.Players.NumPlayers < 2

if game.Players.NumPlayers > 1 then
    Text.Value = "Oh no! There is not enough players to continue..."
else
    Text.Value = "Welcome to the game! A new game is starting in a couple of seconds..."
    wait(10)
    Text.Value = "Picking map..."
    wait(2)
    RandomChoice = math.random(1, NumMaps)
    CurrentMap = Maps[RandomChoice]
    ChosenMap = game.ServerStorage:FindFirstChild(CurrentMap)
    ChosenMap:clone().Parent = game.Workspace
    ChosenMap.Name = "Map"
    Text.Value = "The map selected is : "..CurrentMap.."!"
    wait(3)
    Text.Value = "Preparing your spawn areas..."
    wait(2)
    Text.Value = "Sorted! Joining round..."
    wait(2)
wait(2)
    for i = 40, 0, -1 do
    Text.Value = "Time left : "..i.." seconds"
    wait(1)
end
if Text.Value == "Time left : 0 seconds" then
    Text.Value = "Round over! Well done!"
    wait(5)
    Text.Value = "Resetting game..."
    wait(2)
    _G:Restart()
end
end
0
I do not see your attempt, and you need to be specific when you say "Sort Teams" Shawnyg 4330 — 8y

2 answers

Log in to vote
0
Answered by
KoreanBBQ 301 Moderation Voter
8 years ago

There are many ways to sort into teams, but here's my personal team randomizer (2 teams)

local teamed={}
            local numTeamed=0
            for i,v in pairs(game.Players:GetChildren()) do
                if v.TeamColor==BrickColor.new('Light blue') or v.TeamColor==BrickColor.new('Black') or v.TeamColor==BrickColor.new('White') then
                    numTeamed=numTeamed+1
                end
            end
            for i,v in pairs(game.Players:GetChildren()) do --a loop that goes through all of the players
                if v.TeamColor==BrickColor.new('Light blue') or v.TeamColor==BrickColor.new('Black') or v.TeamColor==BrickColor.new('White') then -- if the player is in trainees team (or on of the teams)
                    local randomposition=math.floor(((tick()*math.random(100))%numTeamed)) -- random number
                    if teamed[randomposition] then
                        repeat randomposition=math.floor(((tick()*math.random(100))%numTeamed)) until not teamed[randomposition]
                    end
                    print(randomposition..' '..v.Name) -- prints that number and name of person
                    teamed[randomposition]=v
                end
            end
            for i,v in pairs(teamed) do
                if i%2==0 then
                    v.TeamColor=BrickColor.new('Black')
                elseif i%2==1 then
                    v.TeamColor=BrickColor.new('White')
                end
            end
0
note that its used for simulations or whatever, im sure you can edit it KoreanBBQ 301 — 8y
Ad
Log in to vote
0
Answered by
KordGamer 155
8 years ago

Shawnyg,

I mean "Sort Teams" as in put players into two teams evenly.

My attempt was this, and no, it doesn't work.

local teamRed = game:GetService("Teams")["Red"]
    local teamBlue = game:GetService("Teams")["Red"]
    function ScrambleTeams()
       local plrs = game.Players:GetPlayers()
       local plrsNew = {}
        --Scramble table:
        while (#plrs > 0) do
            local i = math.random(#plrs)
            table.insert(plrsNew, plrs [i])
            table.remove(plrs, i)
        end
        local teamredcolor = teamRed.TeamColor
        local teambluecolor = teamBlue.TeamColor
      for i,p in pairs(plrsNew) do
        p.TeamColor = teamredcolor or p.TeamColor == teambluecolor
    end
end
    ScrambleTeams()

Answer this question