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

How do I get the number of players in a game?

I want this script to determine how many players are in my game. I want it to update a value, and then I want that value to print so I know the script is working. Output didn't give me anything.

Can someone please help? Here's the script:

local player_count = 0

while wait() do
		local Players = {}
	for _,v in pairs(game.Players:GetPlayers()) do
			table.insert(Players,v)
			player_count = player_count + 1
		end
end

print(player_count)

3 comments

0

Yes: local player_count = game.Players.NumPlayer. You cannot change the value of game.Players.NumPlayer though. NumValue is an int value.

0

How could I get the script to determine how many people are on a certain team?

0

BTW, it's NumPlayers with an s at the end.

1 answer

✓ Accepted answer

There is something called NumPlayers.

print(game.Players.NumPlayer) --That's it. game.Players.NumPlayer.

There is another method that is not as effective.

print(#game.Players:GetChildren()) --# returns the number of things in a table, in this case: the number of children in players.


Hope it helps! (There really is no scripting needed.)



How to see how many players are on a team

local team1 = 0
local team2 = 0
local otherteam = 0
local team1color = BrickColor.new("Bright red")
local team2color = BrickColor.new("Bright blue")

--Maybe a loop?
for _,player in pairs(game.Players:GetChildren()) do
	if player.ClassName == "Player" then
		if player.TeamColor = team1color then
			team1 = team1+1
		elseif player.TeamColor = team2color then
			team2 = team2+1
		else
			otherteam = otherteam+1
		end
	end
end

print("There are "..team1.." players in the red team and "..team2.." players in blue and there are "..otherteam.." left.")

2 comments

0

So for a value, I could say "local player_count = game.Players.NumPlayer"?

1

Yes -- but isn't the property called 'NumPlayers' (plural)? I'm too lazy to check. Just look at the property before coding.

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