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?
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:
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?
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.
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)
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)
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.
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.
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)