Why is the MessagingService always disconnecting before I published a topic?

  1. What do you want to achieve? I want to make a smooth shutdown system which annouces the Reboot Time on all servers from 1 server.

  2. What is the issue? The Messaging Service always disconnects before I even send something over that service. hfeffefe

  3. What solutions have you tried so far? I have already tried on AlvinBlox’s Tutorial and also the Developer Hub (Cross-Server Messaging). But I also have tried changing the FriendServerEvent to GlobalAnnoucements but it did not work.

I am currently only using the code of that article and it did not work.

This is the script of the getting Data Script

local MessagingService = game:GetService("MessagingService")
local Players = game:GetService("Players")
 
local MESSAGING_TOPIC = "FriendServerEvent"
 
Players.PlayerAdded:Connect(function(player)
 
	-- Subscribe to the topic
	local subscribeSuccess, subscribeConnection = pcall(function()
		return MessagingService:SubscribeAsync(MESSAGING_TOPIC, function(message)
			print(message.Data)
		end)
	end)
	if subscribeSuccess then
		-- Unsubscribe from topic upon player ancestry change
		player.AncestryChanged:Connect(function()
			subscribeConnection:Disconnect()
		end)
	end
end)

and this one is from the script which is sending the data:

local MessagingService = game:GetService("MessagingService")
local Players = game:GetService("Players")
 
local MESSAGING_TOPIC = "FriendServerEvent"
 
Players.PlayerAdded:Connect(function(player)
 
	-- Publish to topic
	local publishSuccess, publishResult = pcall(function()
		local message = player.Name .. " joined server with 'JobId' of " .. game.JobId
		MessagingService:PublishAsync(MESSAGING_TOPIC, message)
	end)
	if not publishSuccess then
		print(publishResult)
	end
end)

How can I “fix” that?
Thanks for the answer in advance!

My gut feeling would be that the player ancestry changes shortly after PlayerAdded. You could confirm this by printing before disconnecting.

Why are you using AncestryChanged instead of PlayerRemoving?

Also you will keep subscribing to the same event every time another player joins, even if the existing connection is still there. What’s the purpose of this, as there’s nothing player specific in the connection and even if there was you’d be better off having it connected outside the event.

I have added a printing into

player.AncestryChanged:Connect(function()
			print("Something Needs to go here")
			subscribeConnection:Disconnect()
		end)

and well. It did not print out anything :confused:

I have 2 different scripts. 1 which was from the developer hub which made me ask this question, which is also listed above and a “new” code based on your answer.

The new code’s code looks like this:

local MessagingService = game:GetService("MessagingService")
local Players = game:GetService("Players")
 
local MESSAGING_TOPIC = "GlobalAnnoucements"
 
local subscribeSuccess, subscribeConnection = pcall(function()
		return MessagingService:SubscribeAsync(MESSAGING_TOPIC, function(message)
			print(message.Data)
		end)
	end)

if subscribeSuccess then
	print('Success')
end

game.Players.PlayerRemoving:Connect(function()
			subscribeConnection:Disconnect()
		end)

Players.PlayerAdded:Connect(function(player)
	print('Joined')
end) 

lefoge
This is what it gives out to me when I join the game.
It does print out that I joined, but the service is already disconnected when my message is sent. :confused.

I was using this because the example code on the developer hub was exactly like that.

You don’t need the PlayerAdded or removing events if doing it outside, as your use case is completely unrelated to specific players - shutting down the server should be shown to all players in the servers at that time so can just do Players:GetPlayers at the time or even a remote event with FireAllClients then you don’t have to worry about the player list at all.

Regarding the error, where are you testing this? If it’s in studio then perhaps the API services access in studio is not enabled for that game.

Edit: after a very brief search, MessagingService: Service disconnected

Always use the search bar before posting.

2 Likes

Yes, I was testing it in studio. I never used that MessagingService so I did not know that the MessagingService is not “working” in studio.

Well, sometimes I am a bit too lazy to search. But I got it and I will do that next time!

Thank you for answering :slight_smile:

No problem. The other advice still stands about keeping it outside the events and then when you want to trigger your server shutdown message you can just fire a remote event to all clients which displays the message on their UI or something. Best of luck with the smooth shutdown system