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

Is this statement a correct term for finding players inside of teams?

Asked by
Scythax 35
10 years ago

This is a portion of a script I am making, that's to find the player inside of teams, and divide evenly into 2 teams. But I'm not sure if this is a correct statement. Can someone notify me, if it's invalid, or correct?

if NumPlayers.T1 == 0 then

Message.Text = "T2 wins!"
end

2 answers

Log in to vote
1
Answered by 10 years ago

No, there is no way to get how many players there are directly from a Team object. You'd have to go through each player and see what team they are.

local T1Color, T2Color = T1.TeamColor, T2.TeamColor
local Team1, Team2 = { }, { }
for Index, Player in next, Game.Players:GetPlayers() do --in pairs is fine, too.
    if Player.TeamColor == T1Color then
        table.insert(Team1, Player)
    elseif Player.TeamColor == T2Color then
        table.insert(Team2, Player)
    end
end

Then from there you'd check how many people are on a specific team.

if #Team1 == 0 then
    Message.Text = "T2 wins!"
end
Ad
Log in to vote
0
Answered by 10 years ago

I have solved this problem by making a function that returns the number of players on a team. Code:

local function GetPlayerCountFromTeam(teamname)
        local counter = 0
    local team = game.Teams:FindFirstChild(teamname)
    if team then
        local players = game.Players:GetChildren()
        for count = 1, #players do
            if players[count].TeamColor == team.TeamColor  then
                counter = counter + 1
            end
            wait()
        end
    end
    return counter
end

In order to get the number of players on a team you would do:

local team1 = GetPlayerCountFromTeam("teamname")

You would replace teamname with whatever your team's name is. team1 would be the exact value of people on your team.

Answer this question