My chat tag script doesn't work and textchatservice doesn't either

Hello I created a script for a chat tag and after I found out that I needed to change my old chat service to a new one I started creating a new text chat service and as I created it I started to enter a message into the chat The chat tag with my name doesn’t come out I don’t know what’s wrong help me fix it here’s the script any help is appreciated

local Players = game:GetService('Players')
local TextChatService = game:GetService("TextChatService")
local MarketplaceService = game:GetService("MarketplaceService")

local gamepassId = 951612800 -- The ID of the gamepass that grants VIP status

-- Function to update the chat tags for a player
local function updateTag(player)
    -- Try to find the leaderstats folder and the Status value
    local leaderstats = player:FindFirstChild("leaderstats")
    local tagText = leaderstats and leaderstats:FindFirstChild("Status")
    
    -- If either leaderstats or Status is missing, exit the function
    if not tagText then return end
    
    local color = nil -- Initialize the default color variable

    -- Assign a color based on the player's status value
    if tagText.Value == "Innocent" then
        color = Color3.fromRGB(255, 255, 255)
    elseif tagText.Value == "Evil" then
        color = Color3.fromRGB(189, 83, 83)
    elseif tagText.Value == "Crook" then
        color = Color3.fromRGB(255, 75, 78)
    elseif tagText.Value == "Assassin" then
        color = Color3.fromRGB(77, 11, 14)
    elseif tagText.Value == "Demon" then
        color = Color3.fromRGB(255, 10, 43)
    elseif tagText.Value == "Criminal" then
        color = Color3.fromRGB(255, 53, 56)
    elseif tagText.Value == "Satana" then
        color = Color3.fromRGB(255, 20, 251)
    elseif tagText.Value == "Supervillian" then
        color = Color3.fromRGB(255, 0, 4)
    elseif tagText.Value == "Savior" then
        color = Color3.fromRGB(0, 191, 255)
    elseif tagText.Value == "Guardian" then
        color = Color3.fromRGB(0, 255, 0)
    elseif tagText.Value == "Officer" then
        color = Color3.fromRGB(0, 229, 255)
    elseif tagText.Value == "Defender" then
        color = Color3.fromRGB(20, 255, 20)
    elseif tagText.Value == "Angel" then
        color = Color3.fromRGB(89, 255, 214)
    elseif tagText.Value == "Superhero" then
        color = Color3.fromRGB(0, 100, 255)
    end

    -- Define chat properties for the player
    local chatProperties = {
        NameColor = color, -- Color of the player's name in the chat
        Tags = { -- Tags to display next to the player's name
            {
                TagText = tagText.Value,
                TagColor = color
            }
        }
    }

    -- Check if the player owns the VIP gamepass
    if MarketplaceService:UserOwnsGamePassAsync(player.UserId, gamepassId) then
        -- Insert a VIP tag at the beginning of the Tags list
        table.insert(chatProperties.Tags, 1, {
            TagText = "👑VIP",
            TagColor = Color3.fromRGB(255, 255, 0)
        })
        -- Set the chat message color for VIPs
        chatProperties.ChatColor = Color3.fromRGB(226, 226, 0)
    end

    -- Find the chat user object in TextChatService
    local chatUser = TextChatService:FindFirstChild(player.Name)
    if chatUser then
        -- Apply all the properties to the chat user
        for property, value in pairs(chatProperties) do
            chatUser:SetAttribute(property, value)
        end
    end
end

-- Connect to the PlayerAdded event
Players.PlayerAdded:Connect(function(player)
    -- Wait for the player's character to load
    player.CharacterAdded:Connect(function()
        local leaderstats = player:WaitForChild("leaderstats") -- Wait for leaderstats folder
        local status = leaderstats:WaitForChild("Status") -- Wait for the Status value

        updateTag(player) -- Update the player's chat tags

        -- Listen for changes to the Status value
        status:GetPropertyChangedSignal("Value"):Connect(function()
            updateTag(player) -- Update the chat tags when the status changes
        end)
    end)
end)
1 Like

I tried to write as many comments as possible to understand the script.

1 Like

That’s not how you add chat tags with TextChatService. You need to use the OnIncomingMessage callback on the client and return a TextChatMessageProperties instance.

--services
local tcs = game:GetService("TextChatService")
local players = game:GetService("Players")

--assigning the callback
tcs.OnIncomingMessage = function(message: TextChatMessage): TextChatMessageProperties
    --create the properties
    local properties = Instance.new("TextChatMessageProperties")

    --no TextSource indicates that it wasn't a player that sent it. Might be system message.
    if (not message.TextSource) then
        --return unedited properties
        return properties
    end

    --get the sender
    local player = players:GetPlayerByUserId(message.TextSource.UserId)
    if (not player) then return properties end --if the player doesn't exist, return unedited properties

    --now, we can assign things to the properties. As an example, we can make the user's text yellow.
    properties.Text = "<font color = 'rgb(255, 255, 0)'>"..message.Text.."</font>"

    --return the edited properties to be displayed with the message
    return properties
end

You’ll need to add your chat tag logic, but this is a basic structure.

Also, MarketplaceService:UserOwnsGamePassAsync can fail, you’ll need to use a pcall and error handling in case it does.

Thanks for telling me about this, I was able to fix it by simply writing a script in StarterGui And a local script