MessagingService table contents are nil only on BindToClose

Hi, I’m trying to do a simple server hub system using only MessagingService, it mostly works but I got stuck on the part of deleting the servers.

Hub code:

MessagingService:SubscribeAsync("ServerShutdown", function(serverEncoded: ServerInfo)    
    print("Got new servershutdown event")
    print(serverEncoded)
    print(serverEncoded.code)

    local server = HttpService:JSONDecode(serverEncoded)
    
    print(server)
    print(server.code)
    
    local index = ServerToIndex[server.code]
    
    local NewServers = table.clone(Servers:get())
    table.remove(NewServers, index)
    
    ServerToIndex[server.code] = nil
    
    Servers:set(NewServers)
    
    print("Should be done!")
end)

Server code:

if not RunService:IsStudio() then
    game:BindToClose(function()
        MessagingService:PublishAsync("ServerShutdown", HttpService:JSONEncode({
            code = ServerCode,
        }))

        task.wait(10)
    end)
end

The problem here is that when the server is closing (and only then, all other events work normally), the message gets to the hub but server.code is nil, I have checked and the table does exist. Since I can’t test the game on Roblox studio I have written simple code to get the contents of the table on the console, but that ends up printing nothing.

for _, v in pairs(server) do
       print(v)
end
1 Like

what is ServerCode defined as? have you tried making sure it exists prior to sending the ServerShutdown message?

Yes, it does exist, it is defined on runtime but when that code runs it is already defined. I also tested if it wasn’t a problem with the variable by setting code to a static value and that did not work either.

what happens if you just send the code as a string instead of json?

It does work, the problem with the old code might be the time HttpService takes to encode the table as a json.

Considering that I am not sure how one could proceed to sending a table over MessagingService when the server is about to close.

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