RicHText complication with new TextChatService

I’m attempting to create chat tags based off a ModuleScript stored in ReplicatedStorage. The structure for the Tags module is as follows:

Tags
local Tags = {
	[34103406] = {Tag = "LAWYER", TagColor = Color3.fromRGB(0, 85, 255), ChatColor = Color3.fromRGB(255, 183, 37)}
}

return Tags

My problem stems from me wanting to be able to apply specific group-based tags (see code below) layered over the tag configurations from the Tags module.

Code
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TextChatService = game:GetService("TextChatService")
local Players = game:GetService("Players")

local Tags = require(ReplicatedStorage:WaitForChild("Tags"))

TextChatService.OnIncomingMessage = function(Message: TextChatMessage)
	local Player = Players:GetPlayerByUserId(Message.TextSource.UserId)
		
	local Properties = Instance.new("TextChatMessageProperties")	
	local PropertiesTest = Instance.new("TextChatMessageProperties")
	
	if Player:IsInGroup(5738308) then
		Properties.PrefixText = string.format("<font color='#%s'></font> %s", Color3.fromRGB(30, 30, 30):ToHex(), "[SG]", Message.PrefixText)
	end	
	
	if Tags[Player.UserId] then
		local TagData = Tags[Player.UserId]		
		Properties.Text = string.format("<font color='#%s'>%s</font>", TagData.ChatColor:ToHex(), Message.Text)
		Properties.PrefixText = string.format("<font color='#%s'>[%s]</font> %s", TagData.TagColor:ToHex(), TagData.Tag, Message.PrefixText)
	end

	return Properties
end

https://i.gyazo.com/a76c20870a528c1afe9bdc474c2610f3.mp4

I assume my problem has to do with the tags overwriting eachother but I can’t think of a solution that would allow both tags to work.

Any help would be appreciated.

Since you could have more than 1 tag for a player, you may want to create a list of tags for the sender.
Try something like this instead. We end up only writing to PrefixText once at the very end once we have TagsForSender populated and formatted.

TextChatService.OnIncomingMessage = function(Message: TextChatMessage)
	local Player = Players:GetPlayerByUserId(Message.TextSource.UserId)

	local Properties = Instance.new("TextChatMessageProperties")	
	-- we can use this to "collect" tags for our sender
	local TagsForSender = {}
	
	if Player:IsInGroup(5738308) then
		table.insert(TagsForSender, { Tag = "[SG]", TagColor = Color3.fromRGB(30, 30, 30):ToHex() })
	end	

	if Tags[Player.UserId] then
		local TagData = Tags[Player.UserId]
		table.insert(TagsForSender, { Tag = TagData.Tag, TagColor = TagData.TagColor:ToHex() })
		
		Properties.Text = string.format("<font color='#%s'>%s</font>", TagData.ChatColor:ToHex(), Message.Text)
	end
	
	-- turn Tags into a list of strings
	local formattedTags = {}
	for index, tag in ipairs(TagsForSender) do
		formattedTags[index] = string.format("<font color='#%s'>%s</font>", tag.TagColor, tag.Tag)
	end
	
	local tagsString = table.concat(formattedTags, " ")
	Properties.PrefixText = tagsString .. " " .. Message.PrefixText
	
	return Properties
end
1 Like