How to get a user's default chat colour (simple module)

I’m not sure if this is actually considered a resource as I am not sure if it’s substantial enough but I extracted it and turned it into a basic module for anyone to get the user’s chat colour.

I was wondering how this was done, and it’s mostly math. I frankly am not too sure how it works but here is basically how to use it:

ModuleScript, can be called from server or client. The sole argument should be a string.

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)
end

return module

Example of how to use it (client or server)

local NameColour = require(--[[wherever the module is located]])

ChatService.SpeakerAdded:Connect(function(PlayerName)
	local Speaker = ChatService:GetSpeaker(PlayerName)
    print(NameColour:GetNameColor(Speaker.Name))
end)

Note that I take no credit for this as none of it was written by me. The module is extracted from ExtraDataInitializer.lua, one of the core scripts written by Xsitsu.

Made a game showcasing it:

15 Likes

Why not put the script in ReplicatedStorage? Correct me if i’m wrong but the module doesn’t use any thing that is server-only (like DataStores).

1 Like

ServerScriptService is only available on the server. You’re going to need the speaker which comes from that module, which is a descendant of SSS.

Edit: it’s also a descendant of the Chat service which is a descendant of game. I will try it out and see if it works the same

Why not pass in the player instead and make the module use the player’s name instead of the speaker’s name? A player’s speaker has the same name as the player and the module calculates color based off of the name? That would allow it to be used on the client.

(optionally you could just allow any object with a Name property to be used, or you could just let a string be passed in, not an object)

1 Like

Ah yeah you’re right. I will modify the module to do that. Then if someone’s making a custom chat, they won’t need the default chat modules loaded.

Thanks for the suggestion!

1 Like

Hey, this module looks really useful to me but I’m not sure how to set it to the players’ name colour, this is how I’m calling it:

local NameColor = require(script:WaitForChild("GetNameColorModule"))
local ChatService = require(game.ServerScriptService:WaitForChild("ChatServiceRunner"):WaitForChild("ChatService"))

ChatService.SpeakerAdded:Connect(function(PlayerName)
	local Speaker = ChatService:GetSpeaker(PlayerName)
	print(NameColor:GetNameColor(Speaker.Name))
end)

It’s in ServerScriptService and it works but I’m not sure how to set the colour to the players’ name. Does anyone know?

Not sure what you mean sorry. The :GetNameColor function should return a Color3.

Like, set the user’s chat name colour:
image

I see, you can probably use :SetExtraData on the speaker

local Speaker = ChatService:GetSpeaker(PlayerName)
Speaker:SetExtraData("NameColor", NameColor:GetNameColor(PlayerName))
1 Like