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

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

Asked by 9 years ago

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)
0
Yes: local player_count = game.Players.NumPlayer. You cannot change the value of game.Players.NumPlayer though. NumValue is an int value. EzraNehemiah_TF2 3552 — 9y
0
How could I get the script to determine how many people are on a certain team? CoolJohnnyboy 121 — 9y
0
BTW, it's NumPlayers with an s at the end. EzraNehemiah_TF2 3552 — 9y

1 answer

Log in to vote
1
Answered by 9 years ago

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.")
0
So for a value, I could say "local player_count = game.Players.NumPlayer"? CoolJohnnyboy 121 — 9y
1
Yes -- but isn't the property called 'NumPlayers' (plural)? I'm too lazy to check. Just look at the property before coding. Perci1 4988 — 9y
Ad

Answer this question