How would I get number of players in game?

How would I get the exact amount of players currently playing in the server in a number?
I’ve tried several scripts with such stuff like

game.Players.PlayerAdded:Connect(function()

but it never worked just like required to
It would either get the amount of people in every server or it would just fail.

I mean, there are several ways:

Method 1

localplayerCount = #game.Players:GetPlayers()
Gets the current players in the server

Method 2
game.Players.PlayerAdded:Connect(function(player)
    val.Value = val.Value + 1
end)

game.Players.PlayerRemoving:Connect(function(player)
    val.Value = val.Value - 1
end)

Using an IntValue and increasing/decreasing the player count

Method 3
local players = game.Players:GetChildren()
local player_count = 0
while wait(1) do
    for i, player in pairs(players) do
        player_count = player_count + 1 
        print("There are "..i.." players in the game")
    end
end

Get the current players count from a function.

From scriptinghelpers

Hope it helped!

3 Likes