How do I get a user's default name colour?

Hello!

I am currently working on a chat system and I am wondering how I would get their name colour. I tried this but it always returns nil.

local ServerScriptService = game:GetService('ServerScriptService')
local ChatService = require(ServerScriptService:WaitForChild('ChatServiceRunner'):WaitForChild('ChatService'))

ChatService.SpeakerAdded:Connect(function(PlayerName)
	local Speaker = ChatService:GetSpeaker(PlayerName)
	--Group Prefs
    print(Speaker:GetExtraData('NameColor'))
end)

I believe you have to fork the default chat scripts by copying the scripts on the client in runtime, then study the part responsible for the coloring of their username. It is not arbitrary and seems to have some logic implemented to it based on the name rather than UserId.

2 Likes

Here’s the script I used (if you or anyone else was wondering):

Extracted from ExtraDataInitializer

local module = {}

function module:GetNameColor(speaker)
    NAME_COLORS =
        {
            Color3.new(253/255, 41/255, 67/255), -- BrickColor.new("Bright red").Color,
            Color3.new(1/255, 162/255, 255/255), -- BrickColor.new("Bright blue").Color,
            Color3.new(2/255, 184/255, 87/255), -- BrickColor.new("Earth green").Color,
            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 GetNameValue(pName)
        local value = 0
        for index = 1, #pName do
            local cValue = string.byte(string.sub(pName, index, index))
            local reverseIndex = #pName - index + 1
            if #pName%2 == 1 then
                reverseIndex = reverseIndex - 1
            end
            if reverseIndex%4 >= 2 then
                cValue = -cValue
            end
            value = value + cValue
        end
        return value
    end

    local color_offset = 0
    local function ComputeNameColor(pName)
        return NAME_COLORS[((GetNameValue(pName) + color_offset) % #NAME_COLORS) + 1]
    end
    
    return ComputeNameColor(speaker.Name)
end

return module
1 Like
-- This is better.
local Utilize = {}

-- Player Name Chat Colors
local NameColors = {
	Color3.new(253/255, 41/255, 67/255), -- BrickColor.new("Bright red").Color,
	Color3.new(1/255, 162/255, 255/255), -- BrickColor.new("Bright blue").Color,
	Color3.new(2/255, 184/255, 87/255), -- BrickColor.new("Earth green").Color,
	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,
}

-- Get Name Color
function Utilize.GetNameColor(Username)
	local Value = 0
	local NumName = #Username
	for index = 1, NumName do
		local CValue = string.byte(string.sub(Username, index, index))
		local ReverseIndex = NumName - index + 1
		if NumName % 2 == 1 then
			ReverseIndex = ReverseIndex - 1
		end
		if ReverseIndex % 4 >= 2 then
			CValue = -CValue
		end
		Value = Value + CValue
	end
	return NameColors[(Value % #NameColors) + 1]
end

return Utilize
1 Like