How would I know how many players are in a server

I’m making a script that if there’s a certain amount of player in the server a certain thing will happen.

1 Like

local Players = game:GetService(“Players”)

while task.wait(Amount of time) do
local playersCount
local playersTable = players:GetPlayers()
for i = 1, #playersTable do
playersCount += 1
end
if playersCount > 5 then
some functions
end
playersCount = nil
end

With > 5 was just example, you can do anything

You can use a simple “GetPlayers” method, which will return all players in a table.

local plrs = game:GetService("Players"):GetPlayers()

game:GetService("Players").PlayerAdded:Connect(function()

    plrs = game:GetService("Players"):GetPlayers()
    
    local plrsInServer = #plrs

end)

We use #plrs because #myTable returns the number of values in that table.

local certainNumberOfPlayers = 10
if certainNumberOfPlayers == #game:GetService("Players"):GetPlayers() then
    print("There are 10 players here!")
    -- do your event
end