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

How would I get 1 player per team on player added?

Asked by
ElBamino 153
3 years ago

Hey scriptinghelpers, I have a max 10 players with 10 teams, and I'm looking to put 1 player per team. From what I understand on https://developer.roblox.com/en-us/api-reference/class/Team it says "When a Player joins a game, they will be allocated to the team with Team.AutoAssignable set to true that has the fewest players. If no auto assignable team is available, Player.Neutral will be set to true". Since I can't test it myself for two reasons, my computer is NOT fast enough to run 10 different players (I'm just a poor boy doing this as a hobby) and, it will only let me test 8 different players. How would I achieve my goal of putting 1 player per team (10 max players 10 teams). Does it do automatically in my case or, do I need a script for this?

Thanks for the help in advance, I really appreciate it. I'm only here to learn! Side note: A lot of the stuff I'm doing now I'm extremely new to it so I don't actually know everything yet. This would be my first time actually using teams. Also somebody might say "this isn't a scripting question" but, it actually is. I'm wondering if I need scripting for it or, of it's automatic. I don't understand since, I can't test it and I don't have friends who will test it with me.

I've actually been looking at sample code and models but, I'm not finding the answer to my question. It would be on player added (like it says in the title) NOT on player touched because, this isn't for a tycoon. I need the colors from the teams for a tool I'm working on. Meaning I'm assigning a different color to each player.

1 answer

Log in to vote
1
Answered by
NGC4637 602 Moderation Voter
3 years ago
Edited 3 years ago

I think you might need a script. it randomly assigns a team, even if the team has a player alr.

So perhaps you can put this script in server script service?:

local Teams = game:GetService("Teams")
assignableTeams = {} 

for i,v in pairs(Teams:GetDescendants()) do
    if v:IsA("Team") then
        table.insert(assignableTeams, v) -- for now all teams are assignable so we will add all teams to the table
    end
end

while wait(1) do
    for i,v in pairs(Teams:GetTeams()) do
        local playersInTeam = v:GetPlayers() -- returns all players assigned to the team
        if #playersInTeam == 1 then
            v.AutoAssignable = false -- make it unassignable since it is occupied.
            table.remove(assignableTeams, i) -- the team is occupied, so remove it from the assignable teams table
        end
        if #playersInTeam > 1 then -- if more than 1 players is in a team
            for i,v in pairs(v:GetPlayers()) do
                if i == 1 then
                    print("something here lol") -- skip the first player in the team
                else
                    v.Team = assignableTeams[math.random(1, #assignableTeams)] -- reassign the other player's team
                end
            end
        end             
    end
end
1
so yes, it will automatically do it for you, however you have to watch out since there is a smol chance for the auto assign to put a player's team into a team that is already occupied NGC4637 602 — 3y
0
Hey there NGC4637, thank you for the response and help! I'm getting an error on output on line 9 that states the following. " ServerScriptService.Script:9: invalid argument #2 to 'remove' (number expected, got Instance)", I'm new to tables so, I don't fully understand it. ElBamino 153 — 3y
1
so uh i've edited the script, hope this helps NGC4637 602 — 3y
0
One second, I'm going to test it out. I just got back/home. I'll let you know ASAP. ElBamino 153 — 3y
View all comments (3 more)
0
Ok so, I tested it out. I got an error on output on line 15 that says "ServerScriptService.Script:15: invalid argument #2 to 'remove' (number expected, got Instance) - Server - Script:15". How do I make v in pairs a number instead of instance? I actually tried some stuff out. Would it be #playersInTeam since v is the team? I'm not sure right now. ElBamino 153 — 3y
0
i see my mistake now. I am supposed to select the item's position on the table, so just replace v with i. NGC4637 602 — 3y
0
Hey sorry for the late response. Sweet, it works, thanks NGC4637. I really appreciate the help. ElBamino 153 — 3y
Ad

Answer this question