Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Assign Player to Team on Join?

There are two teams: Red and Black. When a new player joins, they'll be assigned to the team with the least amount of people.

For instance, if Red has 18 people and Black has 22, then the next player that joins will be assigned to Red. If both teams have the same amount of people then the next player that joins will be assigned to a random one.

I couldn't find anything helpful on this (hence the lack of code), so how would I do this? Thanks for your help :)

1 answer

✓ Accepted answer

Help

I suggest reading up on this: https://developer.roblox.com/en-us/api-reference/function/Team/GetPlayers

Code (Server Sided)

local Teams = game:GetService("Teams")

local amountOnRed = 0
local amountOnBlack = 0

for i, v in pairs(game.Players:GetChildren()) do
if v:IsA("Player") then
if v.Team == "Red" then
amountOnRed = amountOnRed + 1
elseif v.Team == "Black" then
amountOnBlack = amountOnBlack + 1
end
end
end

game.Players.PlayerAdded:Connect(function()
if amountOnRed > amountOnBlack then
player.Team = "Black"
elseif amountOnBlack > amountOnRed then
player.Team = "Red"
else
player.Team = math.random(Teams) -- in case the teams are even
end)

Conclusion If this helped please mark as a solution and follow my roblox!

1 comment

0

Also the code might not work properly, as I typed it off the bat, so try to write it in your own words.

This discussion is preserved as it was. New answers, comments, and votes are closed.