Messages with RichText posted with DisplaySystemMessage show raw content

Hey there! I am making a command system. I have gotten to the point of handling arguments of commands and am currently stuck on sending a classical red-colored [system]-tagged error message.

I tried to use DisplaySystemMessage in the RBXSystem channel for that; however, it did not work, and as the title suggests, showed raw content (e.g. <font color… is seen). (Neither did it work with RBXGeneral channel)

The DisplaySystemMessage is ran in a LocalScript in StarterPlayerScripts. The following part gets executed on a client event (Event.OnClientEvent):

game:GetService("TextChatService").TextChannels:FindFirstChild('RBXGeneral'):DisplaySystemMessage(`[SYSTEM] {arg}`, 'systemMessage')

I also have another script called UserChatScript, also in StarterPlayerScripts. Here are its contents:

local function incomingMessage(message: TextChatMessage)
	local textChannel = message.TextChannel
	local properties = Instance.new("TextChatMessageProperties")
	local textSource = message.TextSource

	-- Tags
	if message.TextSource then
		local player = game:GetService('Players'):GetPlayerByUserId(message.TextSource.UserId)
		
		if player:GetAttribute('isOwner') then
			properties.PrefixText = "<font color='#F5CD30'>[Owner]</font> " .. message.PrefixText
		elseif player:GetAttribute('IsDev') then
			properties.PrefixText = "<font color='#F5CD30'>[Developer]</font> " .. message.PrefixText
		elseif player:GetAttribute('isAdmin') then
			properties.PrefixText = "<font color='#F5CD30'>[Admin]</font> " .. message.PrefixText
		end
	end


	if textChannel and textChannel.Name == "RBXGeneral" and message.Metadata == 'systemMessage' then
		properties.Text = string.format("<font color='#FF0000'>%s</font>", message.Text)
		--properties.Text = message.Text
	end
	
	return properties
end

game:GetService("TextChatService").OnIncomingMessage = incomingMessage

How the raw DisplaySystemMessage message gets outputted:
image

I hope there is an answer to my issue and it can be resolved swiftly. Thanks in advance!

2 Likes

Is it possible you’re using characters such as < and > instead of &lt; and &gt;?

1 Like

Thank you for your reply. As you can see in the picture provided in the post, the RichText contents work fine with the PrefixTest, however, as far as I understand this, there is an unexpected behavior with Text.

I tried what you said, it still outputs raw text:
image

I mean &gt; and &lt; for the text content:

local text = "<font color=\"rgb(255,0,0)\"> 10 &lt; 20 </font>"
1 Like

It looks like you replaced the wrong ones. You should replace these instead:

@sozzly @HugeCoolboy2007 Thank you for your replies. It doesn’t work with simple text either:

game:GetService("TextChatService").TextChannels:FindFirstChild('RBXGeneral'):DisplaySystemMessage(`test`, 'systemMessage')

image

Try to just run this code and send what comes out

game:GetService("TextChatService").TextChannels:FindFirstChild('RBXGeneral'):DisplaySystemMessage("<font color='#FF0000'>test</font>", 'systemMessage')

image

Thanks. I’ve tried the piece of code you attached, and it worked; thank you. This is the result:
image

Apparently, the issue was appearing with special characters, like @HugeCoolboy2007 and @sozzly also said.

local function convertToHtmlEntities(inputStr)
    local htmlEntities = {
        ["&"] = "&amp;",
        ["<"] = "&lt;",
        [">"] = "&gt;",
        ['"'] = "&quot;",
        ["'"] = "&apos;"
    }
    
    local outputStr = inputStr:gsub("[&<>'\"]", function(c)
        return htmlEntities[c]
    end)
    
    return outputStr
end

Then just passing the string to the function and using DisplaySystemMessage on that works like a magic:

local arg = convertToHtmlEntities(`[SYSTEM] {arg}`) 
game:GetService("TextChatService").TextChannels:FindFirstChild('RBXSystem'):DisplaySystemMessage(`<font color='#FF0000'>{arg}</font>`, 'systemMessage')

I also changed a few lines in UserChatScript:

if textChannel and textChannel.Name == "RBXSystem" and message.Metadata == 'systemMessage' then
	properties.Text = message.Text
end

Thank y’all

1 Like

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