A script that merges empty servers to make bigger ones

My game runs best with near-full servers, but Roblox’s current behavior spawns new servers seemingly at random.

I wrote this script which will:

  • When a new server starts, check if there’s room in other running servers
  • If the server falls below a minimum number of players, try and move everyone to a fuller server.

Consider carefully whether your game requires this! Not every game needs to run with full servers, and switching players around will harm your retention. Only use this script if you’re confident that the pros out-weigh the cons.

This is a model version with a UI. Just place it in ServerScriptService:
Server Collapsing Script - Creator Store

Alternatively, you can use the following stand-alone code; it does not include a UI:

local maxPlayersToTeleport = 10 -- If the server has more than this many players, never collapse it - even if new.
local minPlayers = 6 -- The bare minimum playercount for the game to be remotely enjoyable. Will force collapse servers under this count.

if game.PrivateServerOwnerId == 0 then -- Does not run in Private Servers
	
	local validServer = false -- General indicator for "should not collapse"

	local MessagingService = game:GetService("MessagingService")
	local Players = game:GetService("Players")

	local connection = MessagingService:SubscribeAsync("ServerStatus", function(msg)
		if validServer then return end
		local info = msg["Data"]
		if #game.Players:GetPlayers() < maxPlayersToTeleport  then -- This primarily exists to avoid collapsing servers after a restart
			if info["Place"] == game.PlaceId -- Make sure it's the same type of server (i.e. not merging text/VC)
				and info["PlayerCount"] < info["MaxPlayers"] -- Making sure there is room for everyone:
				-(game.Players.MaxPlayers-game.Players.PreferredPlayers) -- Factoring in the Social Slots
				-#game.Players:GetPlayers() -- ...and finally, however many folks are in this server.
				and info["PlayerCount"] >= #game.Players:GetPlayers()  -- Is the new server *bigger than* this one?
			then
				local teleportOptions = Instance.new("TeleportOptions")
				teleportOptions.ServerInstanceId = info["Server"]
				for i,v in pairs(game.Players:GetChildren()) do -- We have to teleport individually because you can't TP a group to a specific server
					game:GetService("TeleportService"):TeleportAsync(game.PlaceId, {v}, teleportOptions)
				end
			else
				print("That server wasn't valid!", info["PlayerCount"], info["MaxPlayers"])
			end
		else
			print("This server is too big to collapse!")
		end
	end)

	task.wait(15) -- If it's not a new server anymore, we get a bit more restrictive...
	
	validServer = true

	while true do
		task.wait(10)
		local p = #game.Players:GetPlayers()
		if p >= minPlayers then -- Is this server over the "minimum playable count" size?
			validServer = true -- Then it's valid!
			if p < game.Players.MaxPlayers-(game.Players.MaxPlayers-game.Players.PreferredPlayers) then -- We don't want people teleporting to full servers though!
				MessagingService:PublishAsync("ServerStatus", {
					["Place"] = game.PlaceId,
					["PlayerCount"] = #game.Players:GetPlayers(),
					["MaxPlayers"] = game.Players.MaxPlayers,
					["Server"] = game.JobId
				})
			end
		else
			validServer = false -- If it's under minimum playable count, we'll attempt to collapse.
		end
	end
end```
10 Likes

I may actually need something like this for my game, I appreciate you free sourcing it.

Doesn’t roblox already have servers filling technologies, in experience setting you already can change the filling technology to maximum filling server but somewhy its not recommended by roblox

because roblox attempts to put the player in the server that best matches their region, setting it to fill ignores that and trys to throw everyone into the same server (you can prob see how that might be a issue)

2 Likes

If you set server fill to max:

  • Sometimes it still doesn’t work
  • It doesn’t leave any open slots, which makes it hard for friends to join each other
  • If you have “peak hours” (most games do), eventually servers will decrease in size without new players to take their place

This script addresses all of those issues.

isnt that the point though

you should use the variable you created earlier in the script