I created this script that uses MessagingService to merge smaller servers into larger ones if there is room available. For some reason it seems to work for a bit but then randomly stops working after some time goes by. I made it so that each server has a random 8 digit ID as a number, and I use this instead of sending game.JobId over and over again because I figured it would keep me further away from breaking the MessagingService limits. I only send the JobId once a server sees a server that it wants to teleport to. It makes a request to that servers unique MessagingService topic which then returns the JobId back to the server so that it can send players there. Why is this randomly stopping!?
if not (game.VIPServerId == "" and game.VIPServerOwnerId == 0) then
script.Enabled = false
wait(100000000)
end
local messagingService = game:GetService("MessagingService")
local teleportService = game:GetService("TeleportService")
repeat
wait()
until game.ReplicatedStorage.ServerId.Value ~= 0
local myID = game.ReplicatedStorage.ServerId.Value
local getData = require(game.ReplicatedStorage.GetData)
local justRequested = false
local justSent = false
wait(5)
--DATA RECIEVER
repeat
local subscribeSuccess, subscribeConnection = pcall(function()
return
messagingService:SubscribeAsync("ServerSize",function(message)
local data = message.Data
local myplayers = game.Players:GetChildren()
if data["ServerID"] ~= myID then
--print(data["ServerID"]..": "..data["PlayerCount"])
end
if data["PlayerCount"] > #myplayers and data["ServerID"] ~= myID and data["PlayerCount"] < 46 and 44-data["PlayerCount"] >= #myplayers and #myplayers <= 18 then
if justRequested == false then
--print("REQUESTING JOB ID FROM: "..data["ServerID"])
justRequested = true
local requestData = {}
requestData["RequesterID"] = myID
messagingService:PublishAsync(tostring(data["ServerID"]),requestData)
wait(20)
justRequested = false
end
end
end)
end)
wait(3)
until subscribeSuccess == true
--REQUEST RECIEVER AND TELEPORT SEND
repeat
local subscribeSuccess, subscribeConnection = pcall(function()
return
messagingService:SubscribeAsync(tostring(myID),function(message)
local data = message.Data
if data["RequesterID"] ~= nil and justSent == false then
justSent = true
--print("JOB ID REQUESTED FROM: "..data["RequesterID"])
local sendData = {}
sendData["JobId"] = game.JobId
messagingService:PublishAsync(tostring(data["RequesterID"]),sendData)
wait(30)
justSent = false
end
if data["JobId"] ~= nil then
--print("RECIEVED JOBID FROM: "..data["JobId"])
local players = game.Players:GetChildren()
for i = 1, #players do
local PD = getData.getDataFromPlayer(players[i])
--if PD and (PD.Queue.Value == "" or PD.Queue.Value == nil) then
teleportService:TeleportToPlaceInstance(13656243663,data["JobId"],players[i])
--end
end
game.Players.PlayerAdded:Connect(function(player)
teleportService:TeleportToPlaceInstance(13656243663,data["JobId"],player)
end)
end
end)
end)
wait(3)
until subscribeSuccess == true
--SEND EVENT
script.Send.Event:Connect(function()
local data = {PlayerCount = 0, ServerID = myID}
local myplayers = game.Players:GetChildren()
data["PlayerCount"] = #myplayers
if #myplayers > 4 and #myplayers < 40 then
messagingService:PublishAsync("ServerSize",data)
end
end)
--SEND EVENT LOOP FIRE
while true do
wait(8)
script.Send:Fire()
end