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

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

Asked by 5 years ago
Edited 5 years ago

I know there is a method somewhere, but my current method is not as efficient.

plrjoined = 0
while true do
Game.Players.PlayersAdded:Connect(function()
plrjoined = plrjoined +1
end)
Game.Players.PlayersAdded:Connect(function()
plrjoined = plrjoined -1
end)
wait()
2
GetPlayers() returns a list, so you can define a variable like "#players" to it to get the number of indices DeceptiveCaster 3761 — 5y
2
print(#game.Players:GetPlayers()) hellmatic 1523 — 5y

2 answers

Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

Use the octothorpe (#):

print(#game.Players:GetPlayers())

The octothorpe is Lua's "counting" operator, which returns the number of players in the Players folder via the GetPlayers() method. If you are the only one in your game (i.e. in Studio) you would get this output:

>> 1

Octothorpes, especially for counting the number of players in a game, are extremely useful for PvP games. Most PvP games require 2 players in order to play them, so you can use the octothorpe to get the number of players.

Because the value returned is a number, you can do this:

local players = game.Players:GetPlayers()
if #players < 2 then return end
if #players >= 2 then
    -- This would be where the game would be scripted
end

Just something to think about while we are on this topic. If you have any questions, please ask them.

Thanks, MCAndRobloxUnited

Ad
Log in to vote
-1
Answered by
LawlR 182
5 years ago
local Players = game:GetService("Players") -- Gets the Players service.
local NumberOfPlayers -- Your variable for the number of players.

function NumberOfPlayers() -- Function to get the number of players.
    local Num = 0 -- Num variable to keep track of amount of players.
    for i,v in pairs(Players:GetPlayers()) do -- Loops through all the Player instances in Players.
        Num = Num + 1 -- Adds 1 to Num if a Player instance is found.
    end -- Ends the loop once it's looped through all of the Player Instances.
    return Num -- Returns the number of players to wherever you call this function.
end

NumberOfPlayers = NumberOfPlayers() -- Setting the number of players variable to what the function returns.
0
-1. Too many comments, makes it harder to read. User#24403 69 — 5y
1
or... print(#game.Players:GetPlayers()) hellmatic 1523 — 5y
0
Comments are explanation of each line - there's nothing wrong with this level of commentary in an explanational piece. fredfishy 833 — 5y
0
Although for the record, this turns an O(1) problem into an O(n) problem - `#game:GetService("Players"):GetPlayers()` is superior in basically every regard, as unsatisfie_d points out. fredfishy 833 — 5y
View all comments (3 more)
0
That explanation was too lengthy EliteRayGawnX2 124 — 5y
0
crap answer DeceptiveCaster 3761 — 5y
0
You didn't know how to get the number of players, which is a very easy thing to do, so I assumed you were a beginner. That is why I wrote so many comments, to try and help you out as much as possible. While unsatisfie_d's comment is a lot shorter and gets the job done, my answer lets you do anything with each player. LawlR 182 — 5y

Answer this question