Really simple question but I can’t for the love of god, find the answer anywhere online. I simply want to make it so that when a player chats, the colour of their username in chat isn’t changed by the team that they’re in.
2 Likes
Compared to the legacy chat system, TextChatService does not provide or expose a convenient method for achieving your goal. To solve this problem, I temporarily reverted to the legacy chat system so I could access its source code. In it contains the algorithm responsible for associating a colour to a username. After forking the algorithm, cleaning it up, and modifying it to function best in this situation, I used it to override the prefix text colour of incoming TextChatMessages. Altogether:
--!strict
local TextChatService = game:GetService("TextChatService")
local Players = game:GetService("Players")
local NAME_TAG_RICH_TEXT = `<font color="#%s">%s</font>`
local NAME_COLORS = {
Color3.fromRGB(253, 41, 67),
Color3.fromRGB(1, 162, 255),
Color3.fromRGB(2, 184, 87),
BrickColor.new("Bright violet").Color,
BrickColor.new("Bright orange").Color,
BrickColor.new("Bright yellow").Color,
BrickColor.new("Light reddish violet").Color,
BrickColor.new("Brick yellow").Color,
}
local function getUsernameValue(username: string): number
local length = #username
local value = 0
for index, character in string.split(username, "") do
local characterWeight = string.byte(character)
local inverseIndex = length - index + 1
if length % 2 == 1 then
inverseIndex -= 1
end
if inverseIndex % 4 >= 2 then
characterWeight = -characterWeight
end
value += characterWeight
end
return value
end
local function getUsernameColorHex(username: string): string
local color = NAME_COLORS[(getUsernameValue(username) % #NAME_COLORS) + 1]
return color:ToHex()
end
local function onIncomingMessage(message: TextChatMessage)
if not message.TextSource then
return
end
local player = Players:GetPlayerByUserId(message.TextSource.UserId)
if not player then
return
end
local textChatMessageProperties = Instance.new("TextChatMessageProperties")
textChatMessageProperties.PrefixText = NAME_TAG_RICH_TEXT:format(
getUsernameColorHex(player.Name),
message.PrefixText
)
return textChatMessageProperties
end
TextChatService.OnIncomingMessage = onIncomingMessage
2 Likes
wow I never would have managed this myself. tysm!
1 Like
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.