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

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

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 comments

2

GetPlayers() returns a list, so you can define a variable like "#players" to it to get the number of indices

2

print(#game.Players:GetPlayers())

2 answers

✓ Accepted answer

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

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.

7 comments

0

-1. Too many comments, makes it harder to read.

1

or... print(#game.Players:GetPlayers())

0

Comments are explanation of each line - there's nothing wrong with this level of commentary in an explanational piece.

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.

0

That explanation was too lengthy

0

crap answer

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.

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