I usually don’t ask here, but this has me baffled, I can usually sort of tell when something I write isn’t going to work, but this just seems like it should work to me, and it doesn’t.
I’m trying to scramble teams, first by putting all players on a third team called “Intermission Team”, then looping through all players, and putting them on the team with the smallest amount of Children (Players on that team).
I would assume that each time the loop runs, and a player is placed in a team, #game.Teams.[Team]:GetChildren() would return the correct number of children, but it does not.
This is the script, in ServerScriptService
for i,v in pairs(game.Players:GetChildren()) do
print(#game.Teams.Reds:GetChildren(),#game.Teams.Blues:GetChildren()) --Always 0,0
if #game.Teams.Reds:GetChildren() > #game.Teams.Blues:GetChildren() then
v.Team = game.Teams.Blues
else
v.Team = game.Teams.Reds
end
wait()
end
You should instead use Team:GetPlayers(), this will return all players on this team in an array. To get the amount you simply put a # symbol in front of it. Here is an example:
print("Team1 has " .. #Team1:GetPlayers() .. " players on it!")
This is because Teams do not hold players under them. The Players service does. You should use the solution above (I actually didn’t know Team instances had GetPlayers)
Additionally, Players has GetPlayers. You can loop through Players:GetPlayers() and count each team’s players (this is how I would’ve originally done this). I’d really recommend not using GetChildren for this type of thing since it can lead to weird results in some cases (when an instance is placed in the Players service for example as well as Players parented to nil).