Help with messaging service

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

the table to be set to the server.

---------------------------------- || Variables || ----------------------------------

local field = {} ; self = setmetatable({},field) ; field.__index = field

local MessagingService = game:GetService("MessagingService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Chat = game:GetService("Chat")
local players = game:GetService("Players")
local DataStore = game:GetService("DataStoreService"):GetDataStore(`GlobalChat`)

local FunctionStorage = ReplicatedStorage:WaitForChild("FunctionStorage")
local topic = "NebulaGlobalChat"

---------------------------------- || Methods || ----------------------------------

function field:Subscribe()
	MessagingService:SubscribeAsync(topic,function(data)
		local template = script.Template:Clone()

		template.ImageLabel.Image = players:GetUserThumbnailAsync(data["Player"].UserId,Enum.ThumbnailType.HeadShot,Enum.ThumbnailSize.Size420x420)
		template.Title = Chat:FilterStringForBroadcast(data["Message"],data["Player"])
		template.Name = data["Player"].Name
		template.Parent = script
		
		return template
	end)
end

---------------------------------- || RBX Signals || ----------------------------------

field:Subscribe()

FunctionStorage.PublishMessage.OnServerInvoke = function(player,data)
	warn(data)
	MessagingService:PublishAsync(topic,data)
end

return field

  1. What is the issue? Include screenshots / videos if possible!

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

Decoding it with a JSONDecode, yes I did.

1 Like

Can you show us what data looks like/give us some info on what it contains?

To me it looks like you’re trying to send a player by your SubscribeAsync call. You can’t send an instance, you can only send strings, booleans, numbers, or tables that contain those.

On a sidenote data contains a table which contains two more fields, Data and Sent (or TimeSent, can’t remember the specific field name). So instead of indexing data itself, you’d index data.Data

Data is this:

{Message = self["UI"].Panel.Pages.Home.GlobalChat.Frame.TextBox.Text, Player = player}
1 Like

Is player a player instance? If that’s the case, you’d have to manually write in the player’s user ID and name in your PublishAsync call.

For filtering text, I’d suggest doing that before the PublishAsync call as well

So for example:

function field:Subscribe()
	MessagingService:SubscribeAsync(topic,function(data)
		local template = script.Template:Clone()

		template.ImageLabel.Image = players:GetUserThumbnailAsync(data.Data.UserId,Enum.ThumbnailType.HeadShot,Enum.ThumbnailSize.Size420x420)
		template.Title = data.Data.Message -- on a sidenote I think this may need to be template.Title.Text
		template.Name = data.Data.Name
		template.Parent = script
		
		return template
	end)
end

---------

FunctionStorage.PublishMessage.OnServerInvoke = function(player,data)
	warn(data)

	MessagingService:PublishAsync(topic, { 
        Message = Chat:FilterStringForBroadcast(data.Message, player); 
        UserId = player.UserId;
        Name = player.Name 
    })
end
1 Like

I will have to test this later on.

1 Like

That worked, thank you so much!

1 Like

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