Nametags in the Game Chat

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)
23 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.

Refer to my solution here:

Note my subsequent post regarding getting the Speaker object using the Chat API rather than listening for PlayerAdded.

1 Like

Theres an opcion first, you need to get all scripts from chat for this you need to press “Test”
When you’re in test mode, go to your explorer and look for Chat, copy everything in chat.
Stop the test and paste the copied scripts and folder.
Now go to the part that says “ChatModules” and press the modulescript called “ExtraDataInitializer”
Next look close from line 143, it would have to say " if not speaker:GetExtraData(“Tags”) then"
And you’ve to paste this…

local player = speaker:GetPlayer()
if player.Name==“YourPlayerNameHere” then
local tags = {
{
TagText = “NameTag”,
TagColor = Color3.fromRGB(“COLOR HERE, RGB COLOR 255,255,255”)
}
}
speaker:SetExtraData(“Tags”, tags)
else
local tags = {
{
TagText = “NameTag for OtherPeople”,
TagColor = Color3.fromRGB(“COLOR HERE, RGB COLOR 255,255,255”)
}
}
speaker:SetExtraData(“Tags”, tags)
end

2 Likes

I got it already. Thanks though.

2 Likes