Leaderboard doesn't get correct player tag colors

  1. What do I want? Well I want a custom leaderboard, as well as the playerTag colors being the same as the ones in the default chat system (Note: I’m trying to make the 2007 Leaderboard).

  2. The problem? I didn’t want the player names blank white, and wanted to give it color, just like how it was in the old leaderboard based on archive evidence.

  3. What did I do about it? I added a color pallete and asked AI to do the job for me, but since it’s AI, the job was unsuccessful.

After that, I remembered a phrase from someone about humans being smarter than AI, so I went to the Dev Forums so that I can probably get my solution.

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

local leaderboardFrame = script.Parent
local playerListTemplate = ReplicatedStorage:WaitForChild("UI"):WaitForChild("PlayerListContainer")

local NameColors = {
	BrickColor.new('Bright red'),
	BrickColor.new('Bright blue'),
	BrickColor.new('Earth green'),
	BrickColor.new('Bright violet'),
	BrickColor.new('Bright orange'),
	BrickColor.new('Bright yellow'),
	BrickColor.new('Light reddish violet'),
	BrickColor.new('Brick yellow')
}

-- Get a leaderstat value
local function getLeaderstat(player, statName)
	local leaderstats = player:FindFirstChild("leaderstats")
	if leaderstats then
		local stat = leaderstats:FindFirstChild(statName)
		if stat and stat:IsA("IntValue") then
			return stat.Value
		end
	end
	return 0
end

-- Get a color for the player's name based on their name value
local function getPlayerChatColor(player)
	local name = player.Name
	local hash = 0
	for i = 1, #name do
		hash = hash + string.byte(name, i)
	end
	local colorIndex = (hash % #NameColors) + 1
	return NameColors[colorIndex].Color
end

-- Refresh the leaderboard UI
local function refreshLeaderboard()
	-- Clear old entries
	for _, child in ipairs(leaderboardFrame:GetChildren()) do
		if child:IsA("Frame") and child.Name:find("_Entry") then
			child:Destroy()
		end
	end

	-- Create an entry for each player
	for _, player in ipairs(Players:GetPlayers()) do
		local entry = playerListTemplate:Clone()
		entry.Name = player.Name .. "_Entry"

		-- Username label
		local usernameLabel = entry:WaitForChild("FriendLabel"):WaitForChild("PlayerName")
		if usernameLabel then
			usernameLabel.Text = player.Name
			usernameLabel.TextColor3 = getPlayerChatColor(player)
		end

		-- Tix label
		local tixLabel = entry:FindFirstChild("Labels"):FindFirstChild("Tix")
		if tixLabel then
			tixLabel.Text = tostring(getLeaderstat(player, "Tix"))
		end

		-- Level label
		local levelLabel = entry:FindFirstChild("Labels"):WaitForChild("Level")
		if levelLabel then
			levelLabel.Text = tostring(getLeaderstat(player, "Level"))
		end

		entry.Parent = leaderboardFrame
	end
end

-- Listen for updates
local function watchPlayer(player)
	-- Refresh when team changes
	player:GetPropertyChangedSignal("Team"):Connect(refreshLeaderboard)

	-- Watch leaderstats
	player.ChildAdded:Connect(function(child)
		if child.Name == "leaderstats" then
			child.ChildAdded:Connect(function(stat)
				if stat:IsA("IntValue") then
					stat:GetPropertyChangedSignal("Value"):Connect(refreshLeaderboard)
				end
			end)
			for _, stat in ipairs(child:GetChildren()) do
				if stat:IsA("IntValue") then
					stat:GetPropertyChangedSignal("Value"):Connect(refreshLeaderboard)
				end
			end
		end
	end)

	-- If leaderstats already exist
	local leaderstats = player:FindFirstChild("leaderstats")
	if leaderstats then
		for _, stat in ipairs(leaderstats:GetChildren()) do
			if stat:IsA("IntValue") then
				stat:GetPropertyChangedSignal("Value"):Connect(refreshLeaderboard)
			end
		end
	end
end

-- Hook events
Players.PlayerAdded:Connect(function(player)
	watchPlayer(player)
	refreshLeaderboard()
end)

Players.PlayerRemoving:Connect(refreshLeaderboard)

-- Start watching existing players
for _, player in ipairs(Players:GetPlayers()) do
	watchPlayer(player)
end

refreshLeaderboard()

Edit: Here’s reference on what I’m trying to make (This is part of a script by the Classicblox kit):

local NameColors = {
	BrickColor.new('Bright red'),
	BrickColor.new('Bright blue'),
	BrickColor.new('Earth green'),
	BrickColor.new('Bright violet'),
	BrickColor.new('Bright orange'),
	BrickColor.new('Bright yellow'),
	BrickColor.new('Light reddish violet'),
	BrickColor.new('Brick yellow')
}
local GetNameValue

local function GetNameValue(Name)
	local Length = #Name
	local Value = 0
	for Index = 1, Length do
		local CharacterValue = string.byte(string.sub(Name, Index, Index))
		local ReverseIndex = Length - Index + 1
		if Length % 2 == 1 then
			ReverseIndex = ReverseIndex - 1
		end
		if ReverseIndex % 4 >= 2 then
			CharacterValue = -CharacterValue
		end
		Value = Value + CharacterValue
	end
	return Value % 8
end

local function GetChatColor(Name)
	return NameColors[GetNameValue(Name) + 1]
end

local LastLabel

for v = 1, 20 do
	local Label = Instance.new('TextLabel', List)
	Label.Name = 'Label'..v
	Label.BackgroundColor3 = Color3.new(163 / 255, 163 / 255, 163 / 255)
	Label.BackgroundTransparency = 0.3
	Label.BorderSizePixel = 0
	Label.Size = UDim2.new(0, 250, 0, 20)
	Label.Visible = false
	Label.Font = 'Cartoon'
	Label.FontSize = 'Size14'
	Label.TextColor3 = Color3.new(1, 1, 1)
	Label.TextXAlignment = 'Left'
	if LastLabel == nil then
		Label.Position = UDim2.new(0, 71, 0, 0)
		LastLabel = Label
	else
		Label.Position = LastLabel.Position + UDim2.new(0, 0, 0, 20)
		LastLabel = Label
	end
end

local Player = {}
local Players = Game:GetService('Players'):GetChildren()

local function UpdateList()
	local Labels = List:GetChildren()
	for Index = 1, #Labels do
		if Labels[Index]:IsA('TextLabel') then
			Labels[Index].Visible = false
		end
	end
	for Index = 1, #Player do
		Label = List:FindFirstChild('Label'..tostring(Index))
		if Label then
			Label.Visible = true
			Label.Text = Player[Index].Name
			Label.TextColor3 = GetChatColor(Player[Index].Name).Color
		end
	end
end

Any help would be appreciated!

Function getPlayerChatColor doesn’t return anything

Try change end it of ) to ):Color3

you can use loop in without ipairs or pairs (and not wasting perfomance)

Like this for i,v in whateverthisis do end

Your colors don’t match chat because you’re 1) hashing the name instead of the UserId, and 2) using a BrickColor palette that isn’t the one Roblox chat uses. The default chat picks from a fixed 8-color Color3 palette using a simple hash, and newer chat logic keys it to UserId so the color doesn’t change if a user renames.

Use this exact palette and hash player.UserId:
– Exact 8-color palette used by classic chat

local CHAT_NAME_COLORS = {
    Color3.fromRGB(196,  40,  28),  -- Bright red
    Color3.fromRGB( 13, 105, 172),  -- Bright blue
    Color3.fromRGB( 75, 151,  75),  -- Earth green
    Color3.fromRGB(136,  62, 161),  -- Bright violet
    Color3.fromRGB(193, 105,  79),  -- Rust/brown used in chat palette
    Color3.fromRGB(218, 133,  65),  -- Bright orange
    Color3.fromRGB(245, 205,  48),  -- Bright yellow
    Color3.fromRGB(236, 144, 196),  -- Light reddish violet
}

local function chatColorForPlayer(player: Player): Color3
    -- Hash UserId so color is stable and matches chat
    local s = tostring(player.UserId)
    local sum = 0
    for i = 1, #s do
        sum += string.byte(s, i)
    end
    return CHAT_NAME_COLORS[(sum % #CHAT_NAME_COLORS) + 1]
end

Then swap your setter:

usernameLabel.TextColor3 = chatColorForPlayer(player)

1 Like

Thanks for the breakdown, however, it still doesn’t give the correct color. My color is orange in the chat, but in the leaderboard, it’s red. I still don’t know what’s causing all of this.

1 Like

NOTE:

I added the reference to what I’m looking for. This is part of a script made by the ClassicBlox Kit which you can find here (Script is called “2006-2010 UI”)

1 Like

i already found out my solution, thank you for helping tho!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.