How to add a chat tag after the player's username?

It’s relatively simple to add a tag before the player’s username in chat, but how would add a tag of sorts after their username?

2 Likes

As far as I know, not possible with built-in TextChatService, only with custom

It is quite literally up to you how to format it:
Here’s a snippet of code from one of my previous games:

function setTag(message: TextChatMessageProperties, displayName: string, tagData: {TagColor: Color3, TagText: string})
	message.PrefixText = string.format("<b><font color='rgb(%d,%d,%d)'>[%s]</font> %s</b><font color='#ffffff'>:</font>",
		tagData.TagColor.R * 255,
		tagData.TagColor.G * 255,
		tagData.TagColor.B * 255,
		tagData.TagText,
		displayName
	)
end

If you wanted to put the tag behind the username, it’s as simple as reformatting the string.

function setTag(message: TextChatMessageProperties, displayName: string, tagData: {TagColor: Color3, TagText: string})
	message.PrefixText = string.format("<b>%s <font color='rgb(%d,%d,%d)'>[%s]</font></b><font color='#ffffff'>:</font>",
		displayName,
		tagData.TagColor.R * 255,
		tagData.TagColor.G * 255,
		tagData.TagColor.B * 255,
		tagData.TagText,
	)
end
local tagData = {
	["developer"] = {
		TagText = "Dev",
		TagColor = Color3.fromRGB(154, 25, 25)
	}
}

function setTag(message: TextChatMessageProperties, displayName: string, tagData: {TagColor: Color3, TagText: string})
	message.PrefixText = string.format("<b>%s <font color='rgb(%d,%d,%d)'>[%s]</font></b><font color='#ffffff'>:</font>",
		displayName,
		tagData.TagColor.R * 255,
		tagData.TagColor.G * 255,
		tagData.TagColor.B * 255,
		tagData.TagText,
	)
end

game:GetService("TextChatService").OnIncomingMessage = function(message)
	local properties = Instance.new("TextChatMessageProperties")
	
	if message.TextSource then
		local player = game:GetService("Players"):GetPlayerByUserId(message.TextSource.UserId)
		setTag(properties, player.DisplayName, tagData.developer)
	end
	
	return properties
end

Brilliant - it works! :smiley:
image
This also got me to figure out what those parameter colons are image

Just not quite sure how string.format("<b>%s <font color='rgb(%d,%d,%d)'>[%s]</font></b><font color='#ffffff'>:</font>", works, as I can’t bring it back to somehting basic as:

local str = "The magic word is %s"
print(string.format(str, "Roblox"))
-- The magic word is Roblox

(from the string documentation https://create.roblox.com/docs/reference/engine/libraries/string#format)

I took a bit to try and work it out what each bit would do


What does the ‘b’ stand for and how does it follow the order of displayname, r,g,b, tagtext and how do those land there? :sweat_smile:

the <b> makes the text bold, </b> ends the text being bold. It just makes the usernames of tagged players stand out more. Personal choice of mine when I wrote that system, you can feel free to remove it.

And you can readup here on rich text markup and strong.format:
https://create.roblox.com/docs/ui/rich-text
https://create.roblox.com/docs/reference/engine/libraries/string#format

1 Like

Change you display name, its that easy frfr

Ah yes, cleaned it up a bit and cut every bit to pieces. Never multiple things after the comma for strings, which is why it following the order of them didn’t get to me ;D

Bit late of a reply, but thanks for the help again!

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