Chat coding: Working with tags and name colours?

I want to make it so I can assign different name colours to different players (userid specific) and add tags to said people. For example;
[Mod][thelolguy301]: Hello world!

I’ve been digging around in the chat coding and have not been able to figure out how to do this. Is there any documentation on how to use the Chat model?

16 Likes

This is done through the Lua Chat API on the server.

  • Require ChatService, which is a ModuleScript found under ServerScriptService.ChatServiceRunner;
  • This returns an object with a particular method you’ll need, which is GetSpeaker, and it takes as its argument the username of the person whose tags/name color you want to change;
  • Calling ChatService:GetSpeaker("sircfenner") will return a ChatSpeaker object, which has another method you’ll need, namely SetExtraData (wiki);
  • This function takes
    key (a string indicating which aspect you wish to change); and
    data (the information you wish to apply - see table):
Key Data type
ChatColor Color3
NameColor Color3
Font Enum.Font
TextSize int
Tags array

A ‘tags’ array can contain several tags. Each tag needs to be a table, and can have two keys:
– TagText (string)
– TagColor (Color3)

Assuming speaker refers to a speaker object you have obtained, this is how you would add a red “VIP” tag:

speaker:SetExtraData("Tags", {{TagText = "VIP", TagColor = Color3.new(1, 0, 0)}})
96 Likes

Should I release my module that I made awhile back that lets you add tags, and change user name color, and text color, as well as adding a new tag to the player by requiring the module from dev console and going and calling the add or remove tag function?

That’s all up to you :slightly_smiling_face:

Would it be helpful and would you guys use it?

1 Like

I know I would.

So, it was working, but it had other code and stuff in it so I tried exporting it to it’s own module and it’s breaking so I’m trying to fix it for you.

Thanks for this! I have just been editing all of the scripts then putting my edited versions in place of the real ones, didn’t know you could just do this.

Hello!

I’ve written up a little script for myself and got it to run in Studio, but can’t get it to work at all in Server Mode.

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)

The Script is a Server Script and is located in ServerScriptService.

1 Like

Hey friend!

It’s possible that the speaker object has not yet been created by ChatService when your event listener is fired. You’ll have to wait for it to be created before proceeding. I wrote up this server script which seems to work:

local Players = game:GetService('Players')
local ChatService = require(game:GetService('ServerScriptService'):WaitForChild('ChatServiceRunner'):WaitForChild('ChatService'))

Players.PlayerAdded:connect(function(player)
	local speaker
	while not speaker do
		speaker = ChatService:GetSpeaker(player.Name)
		wait()
	end
	print('Got speaker')
	speaker:SetExtraData('Tags', {{ TagText = 'Developer', TagColor = Color3.new(1, 0, 1) }})
	print('Set extra data')
end)
1 Like

Hello,
I’ve applied your method and it’s given the following error:

Game Script timeout
Stack Begin
Script ‘ServerScriptService.NameTags’, Line 10
Stack End

The code is now:

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

game.Players.PlayerAdded:connect(function(player)
	if player.UserId == 20415935 then
		local Speaker
		while not Speaker do
			Speaker = ChatService:GetSpeaker(tostring(player.name))
            wait()
		end
		print(Speaker)
		Speaker:SetExtraData("NameColor", Color3.fromRGB(209, 193, 97))
		Speaker:SetExtraData("ChatColor", Color3.fromRGB(178, 248, 229))
		Speaker:SetExtraData("Tags", {{TagText = "Developer", TagColor = Color3.fromRGB(189, 83, 83)}})
	elseif player.UserId == 558247746 then
		local Speaker
		while not Speaker do
			Speaker = ChatService:GetSpeaker(tostring(player.name))
		end
		Speaker:SetExtraData("NameColor", Color3.fromRGB(149, 0, 99))
		Speaker:SetExtraData("ChatColor", Color3.fromRGB(255, 114, 206))
		Speaker:SetExtraData("Tags", {{TagText = "TagText2", TagColor = Color3.fromRGB(198, 0, 129)}})
	end
end)
1 Like

It should be this:

local Speaker
while not Speaker do
	Speaker = ChatService:GetSpeaker(tostring(player.Name))
	if Speaker then
		break
	end
	wait()
end

Not this:

local Speaker
while not Speaker do
	Speaker = ChatService:GetSpeaker(tostring(player.name))
end

By using the first one, you don’t have an infinite loop trying to run over and over again with no delay. As well, by checking if you have the speaker, you can exit the loop, avoiding an extra, unnecessary delay.

4 Likes

But isn’t the condition of the loop being that Speaker is undefined? Hence “while not Speaker do”?

1 Like

Yes, but you have to include wait() to give the speaker a chance to exist. Without the wait(), the code is just checking over and over with no delay, and of course if it didn’t exist the previous time it’s not going to exist the next time if you didn’t wait at all.

The part I added in is just to escape the loop without reaching the wait() again. No point in having a delay when you already got the speaker.

1 Like

Alright thank you. I just noticed I forgot the wait()

I just tried it out and it works perfectly fine.

I also just noticed an error in your code too. player.name should be player.Name

You also don’t need tostring() around player.Name. It’s already a string.

I’d recommend that you use the SpeakerAdded event of ChatChannel to do this, as it guarantees that the speaker exists.

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

ChatService.SpeakerAdded:Connect(function(SpeakerName)
	local UserId = Players:FindFirstChild(SpeakerName).UserId
	local Speaker = ChatService:GetSpeaker(SpeakerName)
	if UserId == 20415935 then
		-- stuff
	elseif UserId == 558247746 then
		-- stuff
	end
end)
16 Likes

Is it also possible to add two separate tags?
[Staff][CommunityName][thelolguy301]

Edit: Figured it out already :wave:

2 Likes