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

Is it possible to get the number of player on a team?

I'm trying to make a script where when you click a GUI button it spawns you on the team with less players. I though about creating two IntValues for each team but again I don't know how you would get a value with that. Could you use :GetChildren with the team name and if you can how do you convert that to a number?

1 comment

0

Have you looked at the AutoAssignable property of a team?

1 answer

You could make a function, that gets how many Players have a certain TeamColor (the team you're counting's) e.g-

function GetMembersOnTeam(Color) -- Function
local Count = 0 -- Starts at 0.

for i, v in pairs(Game.Players:GetPlayers()) do -- Looks through all the Players
if tostring(v.TeamColor) == Color or v.TeamColor == Color then -- Checks Players' Team Colors.
Count = Count + 1 -- Increases value of Count
end
end

return Count
end

then in the Button, something like this-

Teams = Game:GetService("Teams")
Player = script.Parent.Parent.Parent.Parent -- Keep adding ".Parent" until you reach the Player

--// Change these to their names.
Team1 = Teams["Team1Name"]
Team2 = Teams["Team2Name"]


function GetMembersOnTeam(Color)
-- Rest of the function goes here...
end


script.Parent.MouseButton1Click:connect(function()

-- Gets number of TeamMembers
local count1 = GetMembersOnTeam(Team1.TeamColor)
local count2 = GetMembersOnTeam(Team2.TeamColor)

--// If there's more members on one team, then it places the player in the other team. If team numbers are equal, forces the Player into Team1
if count2 > count1 then
Player.TeamColor = Team2.TeamColor
else
Player.TeamColor = Team1.TeamColor
end

-- Respawns character
Player:LoadCharacter()

end)

That should do about what you're looking for. You'll have to change it to their actual Team's Names though.

2 comments

0

okay Thanks for the help!

0

Its not working and there is no output error?

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