The “raw” text is the string that contains all the formatting/control character that get interpreted/formatted by the Rich Text renderer to do whatever its set to do with those tags.
The ContentText, then, is the string of character that actually get displayed, with all the formatting stuff removed.
Unfortunately, that information is irrelevant to your problem, which is actually more basic than that: NPCName
already contains the ‘[’ and ‘]’ characters you’re trying to add back in to it!
Lua patterns treat ‘[’ and ‘]’ as special “character classes”, too, but they don’t nest, which is why you’re getting that weird cut-off behavior with "r]:"
.
print(string.match("[test]: hoorah", "test"))
--> test
print(string.match("[test]: hoorah", "[test]"))
--> t
print(string.match("[test]: hoorah", "[test]:"))
--> nil
print(string.match("[test]: hoorah", "[[test]]:"))
--> t]:
And, the correct version:
print(string.match("[test]: hoorah", "%[test%]:"))
--> [test]:
Doing this “correctly” requires you changing the order of operations a tad. Ideally, your Server script generating the NPC messages should not be doing Rich Formatting on them at all, leaving it for the Clients to handle. The issue here is fundamentally a mildly bad API (why do TextChatMessage
TextSource
s need to be Players? Letting the Server register various custom agents could solve this readily.) that you’re circumventing in an awkward way.
Strip your ConvertToRichText
function from the message being sent by the Server and put that code on the Client: the OnIncomingMessage
callback should receive unformatted text that is just the message itself, e.g. the string "[Soldier]: hoorah blah blah etc etc!!!"
. I don’t know what you’re passing to the Metadata field, but assuming the message.Text
is the above string without any Rich Text decorators, something like this should work as a drop-in replacement for the code in your first post:
local NPCName, NPCMessage = string.match(message.Text, "^(%b[]): (.+)") -- capture NPC name and message separately
if NPCName then
local Properties = Instance.new("TextChatMessageProperties")
Properties.PrefixText = string.sub(NPCName, 2, -2)..": " -- neat little feature of string.sub slicing!
-- Also, puts the space between the : and message here
Properties.Text = getFormattedText( NPCMessage, Color3.new(1,0,0) ) -- do the Rich Text decorating *here*
return Properties
end
Now, obviously, this only does a single fixed color (red) for all NPCs, and ignores all other Rich Text decorators you may or may not have been using. The solution to this is to use the message.Metadata
field! That’s what it’s for, even!
I’m not sure what the Metadata field gets populated with for player messages, but for your NPC or other System messages you can create a custom format with whatever specific details you need to decorate the messages appropriately.
For example, assume that my Server is sending this message:
message.Metadata = "{NPCMessage}<sc>Soldier 76</sc>"
message.Text = "<font color='#FF0000'>hoorah, <i>who wants to live forever!?</i></font>"
My OnIncomingMessage
callback can then (at least partially) be this:
local SystemMessageType, Params = string.match(message.Metadata, "^(%b{})(.+)")
if SystemMessageType then
if SystemMessageType == "{NPCMessage}" then
local Properties = Instance.new("TextChatMessageProperties")
Properties.PrefixText = Params
Properties.Text = message.Text
return Properties
elseif SystemMessageType == ...
...
else -- Not a System Message
...
end
Since the only parameter for the NPC message is their name, I can avoid doing any string manipulation apart from testing if and what kind of System message was received. This is what I meant by my original post – you should control the message as it is being generated, modifying that generation itself, rather than trying to take it apart and re-format it somewhere else.
This still isn’t ideal; it wastes bytes sending the Rich Text decoration characters along with the message itself, but it’s simple and effective and probably not even a problem anyway.
If you need something particularly complicated for the Metadata, you might bother using JSON as the format, as there are loads of libraries out there to convert to and from JSON strings and Lua Tables. The solution I came up with here only parameterized the name, but you could also use it to, for example, force the NPC to play a particular animation for that message or choose whether he sends it like a whisper/DM to the current player or anything else you can think of.