Help with messaging service

Explain to me like a five year old, how to use Messaging Service.

I’ve made a shutdown all servers script, and am just wondering if I am doing this correctly?

local PlaceID = 15605543413

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

local ShutdownEvent = ServerStorage.Events.Shutdown

function ShutdownServer()
	print("HIO")
	local ReservedServer = TeleportService:ReserveServer(PlaceID)
	TeleportService:TeleportToPrivateServer(PlaceID, ReservedServer, Players:GetPlayers())
end

ShutdownEvent.Event:Connect(function()
	MessagingService:PublishAsync("Shutdown", "Update")
end)

MessagingService:SubscribeAsync("Shutdown", ShutdownServer)
3 Likes

Does it work as expected? :face_with_raised_eyebrow:

2 Likes

it should be good, but I recommend using :TeleportAsync instead and iirc messaging service does not guarantee a message to deliver; so some servers might not shutdown

MessagingService allows you to broadcast information to all running servers, including private servers. This is simulated in studio.

You’re using it correctly, however you shouldn’t even be using messages for this. Instead, use the regular Shutdown All Servers button on the website, and detect the shutdown using game:BindToClose().

This allows you to connect to the server closing. Functions must take less than 20-30 seconds, because by then, the server closes. You can use this opportunity to teleport all players to a reserved server. This server can detect that it’s reserved, and later, teleport everybody back.

Sounds like a lot of work? No problem: