How to Cap Players in a Server/Lobby

Hello!!!

In my game I have added a server system where people can join your server. Now, I want to cap the players on that server. So if you click on “1” There will be only one player in your lobby, which is yourself. And if you click on “2” there will be two players in your server, all the way to 5 max players.

How would you do that?

All help would be well appreciated! :grin:

image_2025-01-01_095336915

image_2025-01-01_095420882

The children in 5 are the same in 1, 2, 3 and 4. Also the ImageButton is the button that conrolls the amount of people that can go in a server/lobby.

You could have 5 different places each with a different player count, or just kick excess players / teleport them back, that way you have the opportunity to allow for large lobbies with admin commands or something.

You can’t change the max number of players in your game, it is only in the game settings when creating the game

This is where MemoryStoreService is useful. You can store information about your servers. When the player keys in a server code, you can pull up recent information like the player count of that server. If the player count is equal to the server capacity, then you can remove the option to join the server. Memory-stores are meant for frequent reads and writes, so there shouldn’t be issues with constantly polling the player count, but I think using MessagingService to connect to a server-specific topic is better. Each time a server’s population changes, it can publish to its specific topic these sorts of changes, which your server browser can temporarily subscribe to. This is just the frontline, however. You’ll need to ensure any players that do make it in the server are kicked if the capacity is reached. A more convenient and natural way to handle this is to make sub-places with different default capacities

I have made these two scripts. the server script makes a number value and puts it in a folder inside that script. It also detects when a remote function is invoked, but i am not quite sure how do to when the player leaves the server.

The local script, detects if the player has joined the server, and invokes a remote function to the server.

Scripts

Server:

local ServerScriptService = game:GetService("ServerScriptService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

local MaxPlayersFolder = ServerScriptService.MaxPlayers.MaxPlayersFolder

ReplicatedStorage.MaxPlayers.OnServerInvoke = function(player)
	
	local MaxPlayersValue = Instance.new("NumberValue")
	MaxPlayersValue.Parent = MaxPlayersFolder
	MaxPlayersValue.Value = 1
	MaxPlayersValue.Name = player.Name .. " 's Server"
	
	if MaxPlayersValue then
		return true
	else
		return false
	end


end

ReplicatedStorage.JoinServerServer.OnServerEvent:Connect(function(player)
	local MaxPlayerValue = MaxPlayersFolder:FindFirstChild(player.Name .. " 's Server")
	if MaxPlayerValue then
		MaxPlayerValue.Value = MaxPlayerValue.Value + 1
		
		if MaxPlayerValue.Value >5 then
			
		end
	end
end)

Local script


local ReplicatedStorage = game:GetService("ReplicatedStorage")

ReplicatedStorage.CreateServerPlayer.OnClientEvent:Connect(function(player)
	local result = ReplicatedStorage.MaxPlayers:InvokeServer(player)
	task.wait()
end)

The thing is I don’t want to create 5 differnet places, I have put in so much work into the ui and scripting to get to this point, and If i just do that, it would be work wasted.