Migration to new TextChatService problems with chat Tags

I was trying to migrate my game to the new textchatservice, but doing so broke my chat tags I had for myself and some gamepass owners.
I tried to fix it but couldnt, any help?

local TextChatService = game:GetService("TextChatService")
local function IsDonator(Player)
	if not Player then return 0 end
	local count = 0
	for i, v in pairs(GamepassList) do
		if MS:UserOwnsGamePassAsync(Player.UserId, v) then
			count = count + 1
		end
	end
	return count
end
game:GetService("Players").PlayerAdded:Connect(function(Player)

	local AdminCheck, Find = IsAdmin(Player) 


	Player.Chatted:Connect(function(message)
		
		if AdminCheck then
			local formattedMessage = "[Owner] " .. message
			TextChatService:SendMessage(formattedMessage, Enum.TextChatColor.Red, Player)
			return 
		end

	
		local DonatorRank = IsDonator(Player)
		if DonatorRank > 0 then
			local rankTag = string.format("[⚡%d]", DonatorRank)
			local formattedMessage = rankTag .. " " .. message
			TextChatService:SendMessage(formattedMessage, Enum.TextChatColor.Yellow, Player)
			return
		end

	
		TextChatService:SendMessage(message, Enum.TextChatColor.White, Player)
	end)
end)
function IsAdmin(Player)
	if not Player then return end
	local Find = Names[Player.UserId]
	if Find then
		return true, Find
	else return false
	end
end

local DonatorRank = IsDonator(Player)
			if DonatorRank > 0 then
				Rank.Text = string.format("Donator Rank %d", DonatorRank)
				Rank.TextColor3 = Color3.fromRGB(245, 205, 48)
			end

these are pieces of my complete script that have to do with the tagging system, this is also a system to create a tag above player heads in-game.
But that works, only trouble is chat tags

Assuming by ‘Chat Tags’ you mean prefixes to the player’s name in the chat box?

TextChatService.SendMessage doesn’t exist.

I think what you’re looking for is the TextChatService.OnIncomingMessage callback, which you can use to apply chat tags. It must be used on the client.

local tcs = game:GetService("TextChatService")
local players = game:GetService("Players")

tcs.OnIncomingMessage = function(message: TextChatMessage): TextChatMessageProperties
	--create the properties
	local prop = Instance.new("TextChatMessageProperties")
	
	--get player
	local sender = message.TextSource and players:GetPlayerByUserId(message.TextSource.UserId)
	
	if (not sender) then --it's a system message
		return prop
	end
	
	--you can do certain checks for the player here and assign tags appropriately.
	--let's say you wanted an orange chat tag:
	prop.PrefixText = "<font color = 'rgb(255, 170, 0)'>[Orange]</font>["..sender.Name.."]:"
	
	--just return it at the end
	return prop
end
1 Like

so after some playing around i’ve abandoned the original script and tried to do this seperately
In a localscript in StarterPlayerScripts (which i think is the correct way)

i’ve added this:

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

local OWNER_PLAYER_ID = 98053578  -- My ID

TextChatService.OnIncomingMessage = function(message)
	local sender = message.TextSource and Players:GetPlayerByUserId(message.TextSource.UserId)
	if not sender then return message end

	local properties = Instance.new("TextChatMessageProperties")

	if sender.UserId == OWNER_PLAYER_ID then
		properties.PrefixText = "<font color='rgb(255, 0, 0)'>[Owner]</font>"
	else
		properties.PrefixText = ""
	end

	properties.Text = message.Text
	return properties
end


I apologise if this is really basic or I sound stupid but now my NAME is Owner so text comes out like:
Owner: test test

It’s not stupid at all! We’ve all got to start somewhere when learning new things.

So the reason it only comes out as Owner is because the PrefixText of the Message contains your name to be displayed as a prefix to the message. You’re overwriting that with Owner, which is why your name doesn’t show up and only Owner does. You’ll need to add the name manually, or you can make use of the already existing Message.PrefixText to get it. The choice is yours.

2 Likes

Thank you, it works. Marked you as the solution :slight_smile:

2 Likes

I just tested with 2 different players, For some reason the chats are only visible to the person who send them.
is this because the script is a localscript?
image

If each client has the localscript, it should work for all the clients. It must be a client script because you can only use that callback on the client.

Is it that the whole message isnt showing or just the chat tag? Because you’re assigning a blank prefix if they aren’t thr owner, going off of your prevoous script. Also, you don’t need to assign the message text as the property text. Blank fields on the properties default to the message properties.

1 Like

The whole messages aren’t appearing, not even bubbles are appearing above heads.
This is the script I’m using, I modified it a bit to fit more of my game. It works for the people it should work but yea, only client sided.

local Players = game:GetService("Players")
local TextChatService = game:GetService("TextChatService")
local MS = game:GetService("MarketplaceService")

local OWNER_PLAYER_ID = 98053578  -- My ID
local GamepassList = {9228818, 9248248, 9248266, 9300503, 11534141}

local function IsDonator(Player)
	if not Player then return 0 end
	local count = 0
	for _, gamepassId in pairs(GamepassList) do
		if MS:UserOwnsGamePassAsync(Player.UserId, gamepassId) then
			count = count + 1
		end
	end
	return count
end

TextChatService.OnIncomingMessage = function(message)
	local sender = message.TextSource and Players:GetPlayerByUserId(message.TextSource.UserId)
	if not sender then return message end

	local properties = Instance.new("TextChatMessageProperties")

	local donatorRank = IsDonator(sender)
	if sender.UserId == OWNER_PLAYER_ID then
		properties.PrefixText = "<font color='rgb(255, 0, 0)'>[Owner]</font> " .. sender.Name .. ":"
	elseif donatorRank > 0 then
		properties.PrefixText = "<font color='rgb(255, 170, 0)'>[Donator Rank " .. donatorRank .. "]</font> " .. sender.Name .. ":"
	else
		properties.PrefixText = sender.Name .. ":"
	end

	properties.Text = message.Text
	return properties
end

Ok.

When you use MarketplaceService:UserOwnsGamePassAsync() on the client, you’re only allowed to use it for the local player.

So, when your client receives the callback call for your own message, the request is authenticated and the rest of the code is executed.

But, when you get a callback call for a different player, the function will error and execution will stop, so the properties are never returned, so the message is never sent.

Plus, doing an API call like that every time a message is sent is unneccessary and leaves more room for errors. Instead, I’d recommend assigning the player attribute(s) based on their chat tag eligibility when they first join on the server. Then, whenever the player sends a message, have each client check the attributes.

1 Like

Thank you, This works like a charm! I appreciate it. :smiley:

1 Like