Issue with my script trying to emit message across multiple servers?

Error: https://gyazo.com/99523083297a8f2b5084c41f9b5e6991

local Players = game:GetService("Players")
local httpService = game:GetService("HttpService")
local messagingService = game:GetService("MessagingService")

function callbackFunction(serviceData)
	local decodedData = httpService:JSONDecode(serviceData)

	print(decodedData.sender.." : "..decodedData.message)
end

Players.PlayerAdded:connect(function(player)
	player.Chatted:connect(function(msg) 
		local messageData = {
			sender = player,
			message = msg -- filter message first!
		}
		local encoded = httpService:JSONEncode(messageData)
		messagingService:PublishAsync("Chat", encoded)
	end)
end)

messagingService:SubscribeAsync("Chat", callbackFunction)

SubscribeAsync documentation.

The function given to SubscribeAsync receives a dictionary which contains two keys: Data and Sent. The data you give to PublishAsync is in the Data key, so you just need to change the JSONDecode to use serviceData’s Data key (serviceData.Data).

Another problem, not sure how your console didn’t catch it but maybe that’s because it’s using its tostring, but you can’t pass instances over MessagingService. You’re assigning sender in the messageData dictionary to a player instance… you should probably use their name instead.

-- messageData quick fix
local messageData = {
    sender = player.Name,
    message = msg
}

-- serviceData quick fix
httpService:JSONDecode(serviceData.Data)

FWIW: you don’t need to use JSONEncode as MessagingService accepts variant publishes.

-- PublishAsync can submit variants
messagingService:PublishAsync("Chat", {
    sender = player.Name,
    message = msg
})

-- SubscribeAsync can then read those variants directly
local function callbackFunction(networkMesage)
    local myData = networkMessage.Data
    print(`{myData.sender} : {myData.message}`)
end
1 Like

So I’m testing this in my own server btw. Is that an issue? Cause it’s printing nil and nil:
Does this only work for other servers sending data to my own?

local Players = game:GetService("Players")
local httpService = game:GetService("HttpService")
local messagingService = game:GetService("MessagingService")

function callbackFunction(serviceData)
	local decodedData = httpService:JSONDecode(serviceData.Data)

	--print(decodedData.sender.." : "..decodedData.message)
	print(decodedData.sender)
	print(decodedData.message)
end

Players.PlayerAdded:connect(function(player)
	player.Chatted:connect(function(msg) 
		local messageData = {
			sender = player.Name,
			message = msg -- filter message first!
		}
		messagingService:PublishAsync("Chat", messageData)
	end)
end)

messagingService:SubscribeAsync("Chat", callbackFunction)

It might be because you’re still running JSONDecode on Data that’s already a table, so just access the members of Data directly. Messages will be sent to all servers including the current one.

local function callbackFunction(serviceData)
    local data = serviceData.Data

    print(data.sender)
    print(data.message)
end
1 Like

I figured it out, you can’t pass variable instances through with messaging service it seems. So I removed the actual variable and just pointed to an existing ones data.

My test case shows that the code works as-is with the change I mentioned in my previous post made. I’ve attached a reference picture below and changed the PlayerAdded since my player is already in the server at the time of running the code. Any primitive Lua data type except for functions, threads and userdata should be accepted for PublishAsync.

Just in case that’s any informative in the future when you work with MessagingService.

1 Like

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