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 4 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:

1if numofplayers == 3 then
2 
3--assign one player to red
4 
5--assign one player to blue
6 
7--assign the rest to yellow
8 
9end

etc.

Any help?

2 answers

Log in to vote
0
Answered by 4 years ago
Edited 4 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

01Players = game:GetService("Players")
02 
03blueteam = game.Teams.BlueTeam
04yellowteam = game.Teams.YellowTeam
05redteam = game.Teams.RedTeam
06percentage = {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
07 
08function AssignTeams() -- assumes more than 3 players
09    local playerlist = Players:GetPlayers() -- get players
10    local totalplayers = #playerlist -- total players
11    local redpercent = math.ceil(totalplayers * percentage.red) -- calculate red team
12    local bluepercent = math.ceil(totalplayers * percentage.blue) -- calculate blue team
13    local i = redpercent
14    while i > 0 do
15        playerlist[i].Team = redteam
View all 28 lines...
0
I shall give it a test Harryizboss24 8 — 4y
0
Errors on line 9, changed it to "game.Players:GetPlayers()" but then it errors on line 20. Harryizboss24 8 — 4y
0
Oops I butchered my math .5+.5+.9 = 1 oops superbolt999 36 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

Did some of my own scripting and fixed it myself:

01local plrs = game.Players:GetPlayers()
02 
03 
04for i, plr in pairs(plrs) do
05    local p = i/#plrs
06    if p < 0.4 then -- 40%
07 
08        plr.Team = game.Teams.RedTeam
09        plr.Character.Humanoid.Health = 0
10 
11    elseif p < 0.4 then -- 40%
12 
13        plr.Team = game.Teams.BlueTeam
14        plr.Character.Humanoid.Health = 0
15 
View all 22 lines...

Answer this question