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

How do I count how many players there are in a game?

Asked by
wddd89 35
10 years ago

Well, I need to know how to make it so the script knows how many players there are in the game, and if there's not enough it'll say "more members required to play." Yeah, this is of use in one of those "mini-games" games. It'll require a specific amount of players, let's just say four for now.

if Game.Players.GetChildren()
--stuck here lol

5 answers

Log in to vote
2
Answered by
Azarth 3141 Moderation Voter Community Moderator
10 years ago
local mini = 4

while wait(1) do 
    if game.Players.NumPlayers < mini then 
        print("Waiting for ".. mini - game.Players.NumPlayers.. " more player(s) to join."
    end
end

0
Hey. When I use numplayers or any other type of player-counting function, it always gives me zero when I'm testing. Is this some sort of testing thing or is it a glitch? ZetaReticuli 38 — 7y
Ad
Log in to vote
2
Answered by
samfun123 235 Moderation Voter
10 years ago

If you want to check the number of players just do :

players = #game:GetService("Players"):GetChildren()
Log in to vote
1
Answered by
Kratos232 105
10 years ago

There are quite a few ways to count all the Players. Personally, I just use the Property in game.Players NumPlayers. It has the exact number of Players on the server.

For example:

Players = Game.Players.NumPlayers

But I guess another way is:

Players = #Game.Players:GetChildren()
--or
Players = #Game.Players:children()

but that way gets things like String Values and anything else n Players, so it's better to use this:

Players = #Game.Players:GetPlayers()

But I guess if you want to make a more complex way of counting all the players and completely waste your time, you could make functions for when Players Join and Leave and then Add/Minus 1 each time, like this:

~~~~~~~~~~~~~~~~~ NumberOfPlayers = 0

function NewPlayer(New) NumberOfPlayers = NumberOfPlayers + 1 end Game.Players.PlayerAdded:connect(NewPlayer)

function LessPlayer(Old) NumberOfPlayers = NumberOfPlayers - 1 end Game.Players.PlayerRemoving:connect(LessPlayer) ~~~~~~~~~~~~~~~~~

but I prefer to use Game.Players.NumPlayers, because it's a lot easier. Well, I hope this was useful at all.

** - Kratos232**

Log in to vote
0
Answered by
RubenKan 3615 Moderation Voter Administrator Community Moderator
10 years ago

This is not so hard at all, just do the following:

if #game.Players:GetChildren() >= numberYouNeed then
--code
end
Log in to vote
0
Answered by
modFrost 130
10 years ago
local Ammount = 0
if #game:service'Players':GetPlayers() < Ammount then
--hue
end

Answer this question