I’m trying to make it so there are only a set amount of servers open for my game at a time, let’s say the maximum is 3. This means if there are already 3 servers running and a 4th one gets opened then the 4th one will get shut down automatically and force new players to join the existing 3 servers. I’ve done some experimenting and my code pretty much works, only problem is if a new player joins said 4th server and gets kicked and they don’t close the Roblox client immediately it will also shut down ALL the other running servers.
Here’s the full script in ServerScriptService:
local DataStoreService = game:GetService("DataStoreService");
local DataStore = DataStoreService:GetDataStore("ActiveServers");
local serverLimit = 3;
local activeServers = 0;
local accountedForSelf = false;
local kickMesssage;
local function loadData()
local data;
local success, err = pcall(function()
data = DataStore:GetAsync("Servers");
end)
if success then
if not accountedForSelf then
accountedForSelf = true;
activeServers = data + 1; --Include this server in the total count
else
activeServers = data;
end
else
activeServers = 1;
end
end
local function saveData()
local success, err = pcall(function()
DataStore:SetAsync("Servers", activeServers);
end)
if not success then
wait(5);
saveData();
end
end
local function shutdownServer()
activeServers = activeServers - 1;
saveData();
game.Players.PlayerAdded:Connect(function(newPlayer)
newPlayer:Kick(kickMessage);
end)
for each, player in pairs(game.Players:GetPlayers()) do
player:Kick(kickMessage);
end
end
local checkServerIntegrity = coroutine.wrap(function()
while wait(30) do
loadData();
saveData();
if activeServers > serverLimit then
shutdownServer();
end
end
end)
local function initialize()
if serverLimit == 1 then
kickMessage = "There can only be 1 public server active at a time! Please join a current server.";
else
kickMessage = "There can only be "..serverLimit.." public servers active at a time! Please join a current server.";
end
if game.PrivateServerOwnerId == 0 then --We don't care about extra private servers
checkServerIntegrity();
end
end
initialize();
Any help is appreciated. I’m aware that limiting servers is unorthodox but it is necessary for my game.