My leaderboard tags won't work

  1. I am trying to make chat tags my for lb

  2. I have to wait for the leaderboard to update then it will give me the tag and if it updates 2 time it will dup my tag and i don’t want that i want the tag when i join and for it to not dup

  3. I tried removing tags then adding back never worked.

If any one knows how to fix it please help me thanks!
–Main Script–

local statsName = "Wins"
local maxItems = 100
local minValueDisplay = 1
local maxValueDisplay = 9000000
local abbreviateValue = true
local updateEvery = 30
local headingColor = Color3.fromRGB(25, 181, 254)

local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local DataStore = DataStoreService:GetOrderedDataStore("Wins")
local Frame = script.Parent.Frame
local Contents = Frame.Contents
local Template = script.objTemplate

local ChatTags = require(game:GetService("ServerScriptService"):WaitForChild("ChatTags"))

local COLORS = {
	Topaz = Color3.fromRGB(255, 215, 0),
	Silver = Color3.fromRGB(192, 192, 192),
	Bronze = Color3.fromRGB(205, 127, 50),
	Sapphire = Color3.fromRGB(0, 0, 222),
	Jade = Color3.fromRGB(20, 198, 0),
	Amethyst = Color3.fromRGB(198, 31, 213),
	Ruby = Color3.fromRGB(212, 0, 0),
	Obsidian = Color3.fromRGB(0, 0, 0),
	Tap = Color3.fromRGB(255, 255, 255),
	Celestial = Color3.fromRGB(172, 209, 255),
	Gold = Color3.fromRGB(255, 255, 0),
	Blue = Color3.fromRGB(85, 255, 255),
	Default = Color3.fromRGB(0, 207, 218)
}

local ABBREVIATIONS = { "K", "M", "B", "T" }

local function toHumanReadableNumber(num)
	if num < 1000 then
		return tostring(num)
	end

	local digits = math.floor(math.log10(num)) + 1
	local index = math.min(#ABBREVIATIONS, math.floor((digits - 1) / 3))
	local front = num / math.pow(10, index * 3)

	return string.format("%i%s+", front, ABBREVIATIONS[index])
end

local function getItems()
	local data = DataStore:GetSortedAsync(false, maxItems, minValueDisplay, maxValueDisplay)
	local topPage = data:GetCurrentPage()

	Contents.Items.Nothing.Visible = #topPage == 0

	for position, v in ipairs(topPage) do
		local userId = v.key
		local value = v.value
		local username = "[Not Available]"
		local color = COLORS.Default

		local success, err = pcall(function()
			username = Players:GetNameFromUserIdAsync(userId)
		end)

		if not success then
			warn("Failed to get username for userId:", userId, err)
		end

		if position == 1 then
			color = COLORS.Celestial
		elseif position == 2 then
			color = COLORS.Tap
		elseif position == 3 then
			color = COLORS.Obsidian
		elseif position == 4 then
			color = COLORS.Ruby
		elseif position == 5 then
			color = COLORS.Amethyst
		elseif position == 6 then
			color = COLORS.Jade
		elseif position == 7 then
			color = COLORS.Sapphire
		elseif position == 8 then
			color = COLORS.Bronze
		elseif position == 9 then
			color = COLORS.Silver
		elseif position == 10 then
			color = COLORS.Topaz
		end

		local item = Template:Clone()
		item.Name = username
		item.LayoutOrder = position
		item.Values.Number.TextColor3 = color
		item.Values.Number.Text = position
		item.Values.Username.Text = username
		item.ProfilePicture.Image = "https://www.roblox.com/bust-thumbnail/image?userId=".. userId .."&width=420&height=420&format=png"
		item.Values.Value.Text = abbreviateValue and toHumanReadableNumber(value) or tostring(value)
		item.Parent = Contents.Items

		local player = Players:GetPlayerByUserId(userId)
		if player then  
			ChatTags.Add(player, "#" .. position, color)
		else
			print("No player found with userId", userId)
		end
	end
end

script.Parent.Parent.Color = headingColor
Frame.Heading.ImageColor3 = headingColor
Frame.Heading.Bar.BackgroundColor3 = headingColor

while true do
	for _, player in pairs(Players:GetPlayers()) do
		local leaderstats = player:FindFirstChild("leaderstats")

		if not leaderstats then
			warn("Couldn't find leaderstats for player:", player.Name)
			continue  
		end

		local statsValue = leaderstats:FindFirstChild(statsName)

		if not statsValue then
			warn("Couldn't find", statsName, "in leaderstats for player:", player.Name)
			continue 
		end

		pcall(function()
			DataStore:UpdateAsync(player.UserId, function()
				return tonumber(statsValue.Value)
			end)
		end)
	end

	for _, item in pairs(Contents.Items:GetChildren()) do
		if item:IsA("Frame") then
			item:Destroy()
		end
	end

	getItems()

	wait()
	Frame.Heading.Heading.Text = statsName .. " Leaderboard"
	wait(updateEvery)
	print("All done")
end

–Module–

local ChatTags = {}

local Players = game:GetService("Players")

function ChatTags.Add(player, tagText, tagColor)
    local success, err = pcall(function()
        local ChatService = require(game:GetService("ServerScriptService"):WaitForChild("ChatServiceRunner"):WaitForChild("ChatService"))
        local speaker = ChatService:GetSpeaker(player.Name)
        if speaker then
            local existingTags = speaker:GetExtraData("Tags") or {}
            table.insert(existingTags, {
                TagText = tagText,
                TagColor = tagColor
            })
            speaker:SetExtraData("Tags", existingTags)
            print("Added tag for player", player.Name, ":", tagText)
        else
            warn("No speaker found for player", player.Name)
        end
    end)

    if not success then
        warn("Failed to add tag for player:", player.Name, err)
    end
end

function ChatTags.Remove(player, tagText)
    local success, err = pcall(function()
        local ChatService = require(game:GetService("ServerScriptService"):WaitForChild("ChatServiceRunner"):WaitForChild("ChatService"))
        local speaker = ChatService:GetSpeaker(player.Name)
        if speaker then
            local existingTags = speaker:GetExtraData("Tags") or {}
            for i, tag in ipairs(existingTags) do
                if tag.TagText == tagText then
                    table.remove(existingTags, i)
                    break
                end
            end
            speaker:SetExtraData("Tags", existingTags)
            print("Removed tag for player", player.Name, ":", tagText)
        else
            warn("No speaker found for player", player.Name)
        end
    end)

    if not success then
        warn("Failed to remove tag for player:", player.Name, err)
    end
end

return ChatTags

If anyone knows how to fix it let me know and thanks!

1 Like