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

Team max players on it?

Asked by 10 years ago

So i have 6 spawns different colors its a team, is it possible only 2 players can be on the 1 team? ex, 2 players only at Team Green and so on...

thnx :D

0
Attempt some code first. Not gonna make it for you. NinjoOnline 1146 — 10y
0
Please do not use Scripting Helpers as a request site. However, this time I am able to assist you. Please allow me a moment. Articulating 1335 — 10y

1 answer

Log in to vote
2
Answered by 10 years ago
local max = 2
function getAllTeams(slotsNeeded)
   local teamTab = {}
   for i,v in pairs(Game:GetService('Teams'):GetTeams()) do --For all of the teams, do
       if getTeamCount(v.TeamColor) < max or not slotsNeeded then --If it has less than 2 members or the argument is false then
           teamTab[v.TeamColor] = getTeamCount(v.TeamColor) --Add it to the table
       end
    end
    return teamTab
end

function getRandomIndex(tab) 
    local num = math.random(1,#tab) --Get a random index number
    local num2 = 0
    for i,v in pairs(tab) do
        num2 = num2 + 1 --Add one to the other number 
        if num2 == num then --If the index number equals the other number
            return i --Here is our index
        end
    end
end

function getTeamCount(TeamColor)
    local count = 0
    for i,v in pairs(Game.Players:GetPlayers()) do
         count =  v.TeamColor == TeamColor and count + 1 or count --Find the number of players with the TeamColor 'TeamColor'
    end
    return count
end

function fixTeams()
    for i,v in pairs(Game.Players:GetPlayers()) do --For all of the players 
        if getTeamCount(v.TeamColor) > 2 then --If their team had over 2 players
            v.TeamColor = getRandomIndex(getAllTeams(true)) --Give them a random Team
        end
    end
end

while wait(0) do
    fixTeams()
end

Untested.

0
Hi sir, thank you for the effort.. i will just put this at workspace? and thank you again!! V0ngola 0 — 10y
0
Place it in ServerScriptService. Articulating 1335 — 10y
Ad

Answer this question