Script to get each player's chat name color doesn't work

I’m trying to make a script to change every player’s character torso (R6 game) color to their chat name color.

I found out that I can use the Lua Chat System API to do this. Apparently, it’s supposed to be done with ChatSpeaker:GetExtraData(“NameColor”).

Below is the code I’ve tried so far. It doesn’t work, even though there are no errors. The ChatService.SpeakerAdded event never fires and ChatService:GetChannelList() returns an empty array. I can’t figure out why this is happening. Am I not using the ChatService module correctly? I wanna avoid forking the Chat scripts. Please help.

PS I didn’t use Players.PlayerAdded, because I don’t know if a player’s ChatSpeaker is ready by the time the event fires. The only info I’ve found is that “Each Player connected to the game automatically has an associated ChatSpeaker.”.

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

local ChatServiceRunner = Chat:WaitForChild("ChatServiceRunner")
local ChatService = require(ChatServiceRunner:WaitForChild("ChatService"))

local function onSpeakerAdded(SpeakerName)
	local Player = Players[SpeakerName]

	if Player == nil then return end

	local Speaker = ChatService:GetSpeaker(SpeakerName)
	local NameColor = Speaker:GetExtraData("NameColor")
	
	local function onCharacterAdded(Character)
		local Torso = Character.Torso
		
		Torso.Color = NameColor
	end

	Player.CharacterAdded:Connect(onCharacterAdded)
	
	local Character = Player.Character
	
	if Character == nil then return end
	
	onCharacterAdded(Character)
end

ChatService.SpeakerAdded:Connect(onSpeakerAdded)

for _, ChannelName in ipairs(ChatService:GetChannelList()) do
	local Channel = ChatService:GetChannel(ChannelName)
	
	for _, SpeakerName in ipairs(Channel:GetSpeakerList()) do
		onSpeakerAdded(SpeakerName)
	end
end
1 Like

I never use the Lua Chat System API because I’ve had far too many frustrating experiences with it. Here’s the function that I’ve been using for awhile:

local chatColors = {
    Color3.fromRGB(211, 84, 84),	-- red
    Color3.fromRGB(82, 172, 255),	-- blue
    Color3.fromRGB(73, 202, 95),	-- green
    Color3.fromRGB(188, 105, 235),	-- purple
    Color3.fromRGB(255, 134, 97),	-- orange
    Color3.fromRGB(253, 255, 80),	-- yellow
    Color3.fromRGB(255, 126, 182),	-- pink
    Color3.fromRGB(211, 183, 159)	-- tan
}

local function getChatColor(username)
    local value = 0
    for i = 1, #username do
	    local cv = string.byte(string.sub(username, i, i))
	    local ri = #username - i + 1

	    if #username % 2 == 1 then
		    ri = ri - 1
	    end

	    if ri % 4 >= 2 then
		    cv = -cv
	    end

	    value = value + cv
    end

    return chatColors[(value % #chatColors) + 1]
end

I think I came upon this solution when googling my problem, but having to copy paste Roblox’s code which could theoretically change at any time is not ideal. I’m still wondering why the Lua Chat System API doesn’t work correctly? Am i doing something wrong?