How to check server size in a script?

Hello, I would like to know if a server has the maximum number of players allowed. For example if the server size is 2, when there are 2 players I trigger something.

But I don’t know how to know the server size in a script. If anyone has an answer, thank you.

You can simply create a variable to keep track of the number of Players in your server:

local Players = game:GetService(“Players”)
local playersinServer = #Players:GetPlayers()

if playersinServer > 1 then-- Change to your max server size
 -- Code here.
end
1 Like

Extending the previous reply, you can do this

local Players = game:GetService(“Players”)
local playersinServer = #Players:GetPlayers()

if playersinServer == Players.MaxPlayers then
-- do stuff
end

Players.MaxPlayers returns the maximum amount of players that can be in a server.
Just make sure to update the playersinServer variable whenever people are joining or leaving.

5 Likes

You can write this :

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
	if #Players:GetChildren() > 1 then
		--your function here
	end
end)
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.