PublishAsync not working with SubscribeAsync

There seems to be nothing suspicious wrong with my code but it apparently isn’t working. They both have the same topic name being tied to 1 variable and subscribeAsync comes before any publishAsync.

I’ve check all topics on this matter and I couldn’t find anything.

Admin Module:

local Players = game:GetService("Players")
local MessagingService = game:GetService("MessagingService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TextChatService = game:GetService("TextChatService")
local TextChatCommand = TextChatService.TextChatCommand

local Signals = ReplicatedStorage.Signals
local Remotes = Signals.Remotes

local Admin = {}

Admin.GLOBAL_TOPIC = "SHOUT"

Admin.Init = function()
	MessagingService:SubscribeAsync(Admin.GLOBAL_TOPIC,function(message)
		Remotes.ToClient:FireAllClients("ServerShout",message.Data)
	end)
	TextChatCommand.Triggered:Connect(function(textSource,unfilteredText)
		if not textSource.CanSend then return end
		local msgSplit = unfilteredText:split(" ")
		if textSource.UserId == game.CreatorId then
			print("Successfully sent",msgSplit)
			MessagingService:PublishAsync(Admin.GLOBAL_TOPIC,msgSplit[2])
		end
	end)
end

return Admin

By the way the print statement fires everytime you enter the command.

1 Like

Hi!
I’ve created a revised version of your code that includes error trapping and potential fixes. Let me know if you need any additional help.

local Players = game:GetService("Players")
local MessagingService = game:GetService("MessagingService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TextChatService = game:GetService("TextChatService")
local TextChatCommand = TextChatService.TextChatCommand

local Signals = ReplicatedStorage:WaitForChild("Signals")
local Remotes = Signals:WaitForChild("Remotes")

local Admin = {}

Admin.GLOBAL_TOPIC = "SHOUT"

Admin.Init = function()
    -- Let's add some error handling, console prints, and some fixes to see what's going on.
    local success, err = pcall(function()
        MessagingService:SubscribeAsync(Admin.GLOBAL_TOPIC, function(message)
            print("Received message:", message.Data)
            Remotes.ToClient:FireAllClients("ServerShout", message.Data)
        end)
    end)
    if not success then
        warn("Failed to subscribe to topic:", err)
    end

    TextChatCommand.Triggered:Connect(function(textSource, unfilteredText)
        if not textSource.CanSend then return end
        local msgSplit = unfilteredText:split(" ")
        if textSource.UserId == game.CreatorId then
            print("Successfully sent", msgSplit)
            if msgSplit[2] then
                local success, err = pcall(function()
                    MessagingService:PublishAsync(Admin.GLOBAL_TOPIC, msgSplit[2])
                end)
                if not success then
                    warn("Failed to publish message:", err)
                end
            else
                warn("No message argument; command not fired.")
            end
        end
    end)
end

return Admin

1 Like

Ah yes good idea, I got no warnings though and I tried something like this but saw if it succeeded as well and it also did nothing.

Is this happening in Studio or in Roblox?

I kinda moved on from this project.

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