Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Team Balance Script cant find Player?

Asked by 10 years ago

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.

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 CrispyBrix 113 — 10y
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.. Tkdriverx 514 — 10y

1 answer

Log in to vote
0
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
10 years ago

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
Ad

Answer this question