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()
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.