I just want to make it that when you spawn, it checks which team has the least amount of people and puts you on that team. But the output always gives me this error and I have no clue what it means. "Player is not a valid member of DataModel". Can someone help me fix the code and explain this error? Heres the code:
local teams = game:GetService("Teams") local bluepeeps = game.Teams.Blue:GetChildren() local redpeeps = game.Teams.Red:GetChildren() game.Player.PlayerAdded:connect(function(player) if bluepeeps > redpeeps then player.TeamColor = "Bright Red" else player.TeamColor = "Bright Blue" end end)
This is in an ordinary script in the Workspace just in case that means anything. I'm still new to scripting so I was expecting this to not work I jsut don't know what that error means. Don't even know what the "Datamodel" is.
The game
is a Datamodel instance.
Player objects are children of their Teams, they are all children of the Players service. In order to get how many players there are on a given team, you have to check their TeamColor yourself.
In addition, :GetChildren()
would just return a list. But one list of things isn't bigger than another, so you can't use >
. One list is longer; that is, you can compare their lengths, by saying #blue > #red
.
Here's a function that you could use to get players on a given team:
function getTeam(color) -- Ignores subtlety of Neutral local team = {} for _, player in pairs(game.Players:GetPlayers()) do if player.TeamColor == color then table.insert(team, player) end end return team end
Thus you could check
if #getTeam(BrickColor.new("Bright blue")) > #getTeam(BrickColor.new("Bright red")) then