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

How would I assign players to a new team?

Asked by 3 years ago

What I want to do is:

  • Assign a certain percentage of players to the Red team

  • Assign a certain percentage of players to the Blue team

  • Assign the rest to the yellow team

If I can't do percentages then I would like to just manually setup it like this:

if numofplayers == 3 then

--assign one player to red

--assign one player to blue

--assign the rest to yellow

end

etc.

Any help?

2 answers

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

edit: I messed up the math in the percentage. Also tested the code this time, it should work. change percentage table to change the percentage of people in each team.

I did this for one of my games, I didn't test the code yet so let me know if it doesn't work assuming you have the teams as RedTeam BlueTeam YellowTeam Also this assumes there is at least 3 people in the server

Players = game:GetService("Players")

blueteam = game.Teams.BlueTeam
yellowteam = game.Teams.YellowTeam
redteam = game.Teams.RedTeam
percentage = {blue = .05,red = .05, yellow = .90} -- this has to equal 1,currently 5% of people will be in blue 5% in red 90% in yellow

function AssignTeams() -- assumes more than 3 players
    local playerlist = Players:GetPlayers() -- get players
    local totalplayers = #playerlist -- total players
    local redpercent = math.ceil(totalplayers * percentage.red) -- calculate red team
    local bluepercent = math.ceil(totalplayers * percentage.blue) -- calculate blue team
    local i = redpercent
    while i > 0 do
        playerlist[i].Team = redteam
        i = i-1
    end
    i = bluepercent
    while i>0 do
        playerlist[i+redpercent].Team = blueteam
        i = i-1
    end
    i = totalplayers - bluepercent - redpercent
    while i> 0 do
        playerlist[i+redpercent+bluepercent].Team = yellowteam
        i = i-1
    end
end

0
I shall give it a test Harryizboss24 8 — 3y
0
Errors on line 9, changed it to "game.Players:GetPlayers()" but then it errors on line 20. Harryizboss24 8 — 3y
0
Oops I butchered my math .5+.5+.9 = 1 oops superbolt999 36 — 3y
Ad
Log in to vote
0
Answered by 3 years ago

Did some of my own scripting and fixed it myself:

local plrs = game.Players:GetPlayers()


for i, plr in pairs(plrs) do
    local p = i/#plrs
    if p < 0.4 then -- 40%

        plr.Team = game.Teams.RedTeam
        plr.Character.Humanoid.Health = 0

    elseif p < 0.4 then -- 40%

        plr.Team = game.Teams.BlueTeam
        plr.Character.Humanoid.Health = 0

    else -- 20%

        plr.Team = game.Teams.YellowTeam
        plr.Character.Humanoid.Health = 0

    end
end

Answer this question