SubscribeAsync not working

I have this script where it uses PublishAsync and SubscribeAsync, but it won’t receive the message, does anyone know why?
Script:

game.Players.PlayerAdded:Connect(function()
	messagingService:PublishAsync("GetExternalPlayersReady", "Getting External Players Ready")
end)

messagingService:SubscribeAsync("GetExternalPlayersReady", function()
	Print("Received message")
end)

I am not sure but I think publishasync sends information to all other scripts in the other servers not in the one you are in. If you want to transfer any data threw global scripts or local scripts, use _G.

EDIT: NVM it works in the server. I tested out the script. Here is the one I did.

local messagingService = game:GetService("MessagingService")

game.Players.PlayerAdded:Connect(function()
	messagingService:PublishAsync("GetExternalPlayersReady", "Getting External Players Ready")
end)

messagingService:SubscribeAsync("GetExternalPlayersReady", function()
	print("Received message")
end)

Just checked. The problem is that Printing is written with a lower case at the beggining not an upper one.

Oh, I just noticed that.
But the problem wasn’t the printing, as that wasn’t there in the original script. It just didn’t receive the message at all.

Could I see the real script if it’s not a huge problem?
And may I also know if you get any errors in the console?

1 Like
playersReady = {}
shouldReply = true

messagingService = game:GetService("MessagingService")

game.ReplicatedStorage["Ready Up"].OnServerEvent:Connect(function(playerName, ready)
	game.ReplicatedStorage["Ready Up"]:FireClient(playerName, ready)
	if ready == true then
		messagingService:PublishAsync("CompQueue", tostring(playerName).."905829288")
	else
		messagingService:PublishAsync("CompQueue", tostring(playerName).."914153215")
	end
end)

game.Players.PlayerRemoving:Connect(function(playerLeaving)
	if table.find(playersReady, tostring(playerLeaving)) then
		messagingService:PublishAsync("CompQueue", tostring(playerLeaving).."914153215")
	end
end)

game.Players.PlayerAdded:Connect(function()
	if #game.Players:GetPlayers() == 1 then
		--shouldReply = false
		messagingService:PublishAsync("GetExternalPlayersReady", "Getting External Players Ready")
		--wait()
		--shouldReply = true
	end
end)

messagingService:SubscribeAsync("CompQueue", function(msg)
	if string.find(tostring(msg.Data), "905829288") then
		table.insert(playersReady, string.sub(msg.Data, 1, -10))
	elseif string.find(tostring(msg.Data), "914153215") then
		table.remove(playersReady, table.find(playersReady, string.sub(msg.Data, 1, -10)))
	end
	game.ReplicatedStorage["Total Players Ready"]:FireAllClients(playersReady)
end)

messagingService:SubscribeAsync("GetExternalPlayersReady", function()
	if shouldReply == true then
		messagingService:PublishAsync("ExternalPlayersReady", playersReady)
	end
end)

messagingService:SubscribeAsync("ExternalPlayersReady", function(msg)
	print(msg.Data)
end)

The only problems I have is when a new player joins, and the commented out parts are commented out so I can test it without multiple servers.

1 Like

This code worked for me

local messagingService = game:GetService("MessagingService")

game.Players.PlayerAdded:Connect(function()
	messagingService:PublishAsync("GetExternalPlayersReady", "Getting External Players Ready")
end)

messagingService:SubscribeAsync("GetExternalPlayersReady", function(message)
	print(message.Data)
end)

I think you forgot to change the channel name in the PublishAsync…

This is why you don’t copy and paste your own lines of code :slight_smile:

1 Like

It looks fine to me…
Maybe it’s a studio bug

playersReady = {}
shouldReply = true

messagingService = game:GetService("MessagingService")
local Isloaded = false
coroutine.wrap(function()
	messagingService:SubscribeAsync("CompQueue", function(msg)
		if string.find(tostring(msg.Data), "905829288") then
			table.insert(playersReady, string.sub(msg.Data, 1, -10))
		elseif string.find(tostring(msg.Data), "914153215") then
			table.remove(playersReady, table.find(playersReady, string.sub(msg.Data, 1, -10)))
		else
			print("error")
		end
		game.ReplicatedStorage["Total Players Ready"]:FireAllClients(playersReady)
	end)

	messagingService:SubscribeAsync("GetExternalPlayersReady", function()
		if shouldReply == true then
			messagingService:PublishAsync("ExternalPlayersReady", playersReady)
		end
	end)

	messagingService:SubscribeAsync("ExternalPlayersReady", function(msg)
		print(msg.Data)
	end)
	Isloaded = true
end)()



game.Players.PlayerRemoving:Connect(function(playerLeaving)
	if table.find(playersReady, tostring(playerLeaving)) then
		messagingService:PublishAsync("CompQueue", tostring(playerLeaving).."914153215")
	end
end)

game.Players.PlayerAdded:Connect(function()
	repeat wait() until Isloaded
	if #game.Players:GetChildren() == 1 then
		messagingService:PublishAsync("GetExternalPlayersReady","")
	end
end)

game.ReplicatedStorage["Ready Up"].OnServerEvent:Connect(function(playerName, ready)
	game.ReplicatedStorage["Ready Up"]:FireClient(playerName, ready)
	if ready == true then
		messagingService:PublishAsync("CompQueue", tostring(playerName).."905829288")
	else
		messagingService:PublishAsync("CompQueue", tostring(playerName).."914153215")
	end
end)

The problem here is that the OnPlayerAdded is done first before the SubscribeAsync has loaded.
I changed it so it could wait until the SubscribeAsync gets loaded.

As the table index should get printed as the data.

EDIT: I had to remove an argument as it was unneeded. Try again now.

what do you mean a studio bug? you forgot to right “Get” when you were publishing async here.

messagingService:SubscribeAsync("GetExternalPlayersReady", function()
	if shouldReply == true then
		messagingService:PublishAsync("ExternalPlayersReady", playersReady)

Oh, right.
I completely forgot it wouldn’t load in time for playerjoined, I feel so stupid now.
I’m about to go to sleep so I’ll set this as the solution if it works when I wake up.

The one without the get was sending the information back, I probably should’ve named it something better.
By studio bug I mean when Roblox Studio just doesn’t do what it should, but I think I was just stupid and put things in the wrong order.

Please solution the answer as to close the title for others.

Also, @0k_Jackk the GetExternalPlayersReady and ExternalPlayersReady are to different topics for the Subscribe Service. @BriefYayyayyay has made for both a SubscribeAsync functions so both topics are different.

1 Like

I mean… There’s PublishAsync(“CompQueue”) like three times. I don’t know if that’s intended.