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

Team Balance Script cant find Player?

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.

2 comments

0

I just realized roblox already does this... brainfart. But can someone still explain what I did wrong and the error so at least I learned something from this? XD

0

Well, first of all, players don't get added to the teams themselves. You'll have to count each team with code then check it's number, etc..

1 answer

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

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