Nametags in the Game Chat

Like, I’ve heard there’s a thing to place a nametag on a player in-chat, similar to this:
image

But I just don’t really know, how to do that, where to start, or really anything pertaining to this. Like, if I do this, do I have to include the Roblox chat filter? And how do I do this in a way that I can control exploiters from doing it to themselves? Or is that not even a concern I have to have? I’m just using the Basic Regular Roblox Chat box in the top left of the screen.

5 Likes

Ive done this in the past, I’ll check when I get home since I don’t remember how to do it. I believe it relied on :GetSpeaker()

You can do this using ExtraDataInitializer(I think, on mobile sorry) module in the default chat core GUI.

This model should have the instructions and resources to show you how to do it, let me know if you need any help.

7 Likes

I’ll take a look at it.

You don’t need to have it in a chatservice module, I’ve done it from a serverside script before.

You don’t, however that module is used in the default chat anyway, and the person that made this model decided to include it in that instead of using a Server script.

I’ve found code by @thelolguy301 that you can pick apart to use.

 local Players = game:GetService("Players")
local Storage = game:GetService("ReplicatedStorage")
local ScriptService = game:GetService("ServerScriptService")
local ChatService = require(ScriptService.ChatServiceRunner.ChatService)

local Speaker

game.Players.PlayerAdded:connect(function(player)
	repeat wait() until ChatService
	if player.UserId == 20415935 then
		Speaker = ChatService:GetSpeaker(tostring(player.Name))
		print(Speaker)
		Speaker:SetExtraData("NameColor", Color3.fromRGB(0, 0, 255))
		Speaker:SetExtraData("ChatColor", Color3.fromRGB(0, 255, 0))
		Speaker:SetExtraData("Tags", {{TagText = "Developer", TagColor = Color3.fromRGB(255, 0, 255)}})
	elseif player.UserId == 558247746 then
		Speaker = ChatService:GetSpeaker(tostring(player.Name))
		Speaker:SetExtraData("NameColor", Color3.fromRGB(0, 0, 255))
		Speaker:SetExtraData("ChatColor", Color3.fromRGB(255, 0, 0))
		Speaker:SetExtraData("Tags", {{TagText = "TagText2", TagColor = Color3.fromRGB(0, 255, 0)}})
	end
end)
9 Likes

That is a cleaner version of it indeed, however the model I linked allows for tags for people in certain groups, or for certain UserIds, but that can also be added here with a couple of tables.

For what I’m doing, @Starception’s way will work fine.

Thanks though. In the future, I might use the version you sent if I’m doing more complex stuff.

1 Like

Update:

I’m running this in a Server Script inside ServerScriptService. Not sure if I did something wrong…

The code provided by @Starception relies on a certain module being in ServerScriptService. Unless you have that module, you presumably won’t be able to use that code.

1 Like

That’s what I was thinking, but I wasn’t quite sure. :confused:

Just use the model I linked you.

1 Like

Try replacing the said line to this:

local ChatService = game:GetService("ChatService")

image

The only chat-related service afaik is

game:GetService("Chat")

but, at least from the wiki, it doesn’t seem to provide the same functions.

1 Like

I think I found the scripts you need to run the provided code:
ChatServiceRunner.rbxm (20.8 KB)

1 Like

I’m just going with Lord’s version. Got it up and working already.

1 Like

Hayo!

I know this is very similar to other code, but it’s a little more efficient in some aspects.
Instead of waiting for a player to join, it relies on the ChatService to add a speaker.

This is mainly aimed as an easy way to setup group rank tags in-game. Just plop this into a Script under ServerScriptService. Customize to your liking. :slight_smile:

Note, this also includes an override for a certain UserId, be sure to change it

--[==[ START VARIABLES ]==]--

local Players = game:GetService("Players")
local ScriptService = game:GetService("ServerScriptService")
local ChatService = require(ScriptService:WaitForChild("ChatServiceRunner").ChatService)

--[==[ END VARIABLES ]==]--

--[==[ START TABLE FOR TAGS ]==]--

--[==[ 
	FORMAT:
	["RankInGroup"] = {TagText = "TagTextHere", TagColor = Color3.fromRGB(r, g, b)} 
]==]--

local ranks = {
    ["255"] = {TagText = "Lead Developer", TagColor = Color3.fromRGB(233,30,99)},
    ["250"] = {TagText = "Lead Scripter", TagColor = Color3.fromRGB(230,126,34)},
    ["225"] = {TagText = "Place Developer", TagColor = Color3.fromRGB(230,126,34)},
    ["200"] = {TagText = "Asset Developer", TagColor = Color3.fromRGB(46,204,113)},
    ["150"] = {TagText = "Moderator", TagColor = Color3.fromRGB(32,102,148)},
    ["100"] = {TagText = "Moderator-In-Training", TagColor = Color3.fromRGB(76,188,206)},
}

--[==[ END TABLE FOR TAGS ]==]--


--[==[ GROUP ID ]==]--
local groupId = 3108202
--[==[ GROUP ID ]==]--

ChatService.SpeakerAdded:Connect(function(SpeakerName) -- Wait for a speaker to be added to Chat
	
    if game.Players:FindFirstChild(SpeakerName) then -- Makes sure the speaker is a real player
	
		local plr = game.Players:FindFirstChild(SpeakerName) -- Get the player Object
	
        local UserId = plr.UserId -- Getting the UserId of the speaker
        local Speaker = ChatService:GetSpeaker(SpeakerName) -- Getting the Speaker Object from ChatService

        if plr.UserId == 26666928  then -- Replace 26666928 with your own ID
	
              Speaker:SetExtraData("Tags", {{TagText = "Script Developer", TagColor = Color3.fromRGB(241,196,15)}}) -- Customize the text and color to your liking
              Speaker:SetExtraData("NameColor", Color3.fromRGB(241,196,15)) -- Set the name color to the tag color

        elseif plr:IsInGroup(groupId) then -- If player is in the group, check the rank
	
            local rank = plr:GetRankInGroup(groupId)

            for i,tag in pairs(ranks) do -- Loop through the ranks table
	
                if i == tostring(rank) then -- If the rank of the player is in the ranks table, set the tag accordingly
                    Speaker:SetExtraData("Tags", {{TagText = tag.TagText, TagColor = tag.TagColor}})
                    Speaker:SetExtraData("NameColor", tag.TagColor) -- Sets the name color of speaker to the tag color
                end

            end   
     
        end

    end

end)
22 Likes

If you’re using the default chat, then this happens because the scripts don’t begin there. You have to use WaitForChild when you need something which won’t always exist by the time your script runs.