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

How would I find the amount of players in a team?

I'm making a gameplace where in the games there has to be 1 player left to get points, but I don't know how to locate the amount of players in a team.

pteam = game.Teams.Playing:GetChildren()

...

if pteam.NumPlayers == 1 then
	pteam.leaderstats.Points.Value = pteam.leaderstats.Points.Value + 25

This is in a script

3 comments

0

These are parts of the script, there is an end on the if statement.

0

Before downvoting my answer, want to tell me why it doesn't fit your 'need'?

0

I didn't downvote.

1 answer

✓ Accepted answer

The NumPlayers property is only found under the service Players. To find the number of people on a teams you may have to do a comparison check with the Players TeamColor and the Teams TeamColor.

local function getPlayersOnTeam(tColor)
local plrs = {}
for _, player in pairs(game.Players:getPlayers()) do
if (not player.Neutral) and player.TeamColor == BrickColor.new(tColor) then
table.insert(plrs, player)
end
end
return plrs
end

print(#getPlayersOnTeam(game:GetService("Teams")["Playing"].TeamColor))

If you just want to call the function with only TeamColor, you can do:

local function getPlayersOnTeam(tColor)
local plrs = {}
for _, player in pairs(game.Players:getPlayers()) do
if (not player.Neutral) and player.TeamColor == BrickColor.new(tColor) then
table.insert(plrs, player)
end
end
return plrs
end

local tabOfPlayers = getPlayersOnTeam("Bright Red")
print(#tabOfPlayers) -- Prints number of people on team

Checking For One Player

As per your comment, you are interested in finding one Player it seems. Since we have a table of Players on one team, it's easy to do this!

local function getPlayersOnTeam(tColor)
local plrs = {}
for _, player in pairs(game.Players:getPlayers()) do
if (not player.Neutral) and player.TeamColor == BrickColor.new(tColor) then
table.insert(plrs, player)
end
end
return plrs
end
local num;
local team = game:GetService("Teams")["Playing"];
repeat
local num = (#getPlayersOnTeam(team.TeamColor))
until num ==1 
local plrs = getPlayersOnTeam(team.TeamColor)
if plrs[1]:FindFirstChild("leaderstats") then
plrs[1].leaderstats.points.Value = plrs[1].leaderstats.points.Value + 1
end

Accessing everyone's playerGUI & TextLabel

As per your comment, you are interested in accessing everyones PlayerGui. Believe it or not, this can easily be done with a loop:

for _,player in pairs(game.Players:GetPlayers()) do
if player.PlayerGui:FindFirstChild("Intermission") then
player.PlayerGui["Intermission"].TextLabel.Text = "Intermission Started!"
end
end

Code Above Is Untested

20 comments

0

Why was this downvoted? This answers the OP's question.

0

I upvoted yours becuz I kinda felt bad for ppl downvoting u.

0

Thanks. Someone is going around down-voting all my posts.

0

Where it says tColor, do I put the color of the color of the team there?

0

When you call the function, you put the teamColor. I'll edit my post so you see what I mean.

0

Wait, I see, I don't need to. I saw the part where it said "Playing"

0

So now, if I want it to see if there is 1 player, how would I do that? Where would I put the part of the script where that 1 player gets 25 points?

0

I edited again.

0

Oh, no.. Not player points, the points inside a leaderboard.

0

Edited Again :\

0

Alright! Finally.

0

I'm sorry to say this but... I need to fix one part of the script to see if it works. How would you access all player's playergui?

0

Since I want to do something with the textlabel inside of the screengui, how would I do that? (I'm sorry I'm not really good with this kind of scripting)

0

Alright. What is the name of the ScreenGui and is the Textlabel directly inside it or is it inside a frame thats inside ScreenGui?

0

TextLabel inside screengui. (...Intermission.TextLabel)

0

Edited Again :P

0

I tried it, the whole script didn't do anything, want me to put the whole script?

0

Which script?

0

1 Problem with the game script, I'll put it in a separate question: https://scriptinghelpers.org/questions/16301/random-spawning-not-working

0

Can you guys do me a favor and down-vote everything of this guy: https://scriptinghelpers.org/user/3089/XToonLinkX123

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