Hey there, snazzy tutorial!
Another user posted a tutorial regarding the Lua Chat System earlier which is just how to configure tags in general. Like that post, I have feedback to give so that you can properly use the Lua Chat System’s API to your advantage. I would encourage you to take a look at some of the docs, since they do have some very useful information.
GetService
Please use GetService to remain consistent in how you get services. As some services can be renamed, do not have proper names (e.g. RunService has a space in it) or cannot be accessed through direct indexing, GetService is your friend for remaining consistent in how you fetch services.
Use clear variable names
You want to be clear in what your variables are pointing towards. service
should be MarketplaceService
or anything to indicate that you are working with the aforementioned service. This will better help your code stay readable and accessible. In addition, if you are working with other services, you want that distinction of what service you are going to be working with as you are writing out code. Clear variable names are the best variable names.
Use variables
I noticed that you require the ChatService in the added function. Avoid this! Make the ChatService an upvalue so that you can avoid calling for the service frequently. You will always have it available after the first require. The same goes with the tags table and creating a new table for every player, though I believe this is a non-problem. This would just make everyone’s tag table point to the same one.
local ServerScriptService = game:GetService("ServerScriptService")
local MarketplaceService = game:GetService("MarketplaceService")
local ChatService = require(ServerScriptService:WaitForChild("ChatServiceRunner").ChatService)
local GAME_PASS_ID = 123456
local VIP_TAGS = {
{
TagText = "👑VIP",
TagColor = Color3.fromRGB(0, 255, 255)
}
}
Use the API
The ChatService provides you wonderful APIs that you can use to couple your code with the service rather than with an outside one. Chat tag scripts like this should almost never rely on the Players service. Use SpeakerAdded so you can act based on when the ChatService registers a new player rather than the player connecting to the server. Use ChatSpeaker:GetPlayer() to get the player object, if one is associated. More in the next header.
Don’t use anonymous functions
Avoid direct connections via anonymous functions so you can account for both new and existing players. Store your function in a local variable, connect it then run it in case there are already any existing speakers (this can happen, including for non-player speakers!).
-- speakerAdded fires with the name of the speaker, not players
local function speakerAdded(speakerName)
doCodeOnSpeaker()
end
ChatService.SpeakerAdded:Connect(speakerAdded)
for _, speaker in ipairs(ChatService:GetSpeakerList()) do
speakerAdded(speaker)
end
Putting the feedback all together
With this feedback in mind, you can have a renewed code sample that allows you to set chat tags for players without any added bulk and you can also account for overlooked cases, such as the aforementioned non-player speaker case. Putting it all together, you can have a code sample that looks a bit like the following:
local ServerScriptService = game:GetService("ServerScriptService")
local MarketplaceService = game:GetService("MarketplaceService")
local ChatService = require(ServerScriptService:WaitForChild("ChatServiceRunner").ChatService)
local GAME_PASS_ID = 123456
local VIP_TAGS = {
{
TagText = "👑VIP",
TagColor = Color3.new(0, 1, 1)
}
}
local function speakerAdded(speakerName)
local speaker = ChatService:GetSpeaker(speakerName)
local player = speaker:GetPlayer()
-- ChatSpeaker belongs to a player entity
if player then
if MarketplaceService:UserOwnsGamePassAsync(player.UserId, GAME_PASS_ID) then
speaker:SetExtraData("Tags", VIP_TAGS)
speaker:SetExtraData("ChatColor", Color3.fromRGB(0, 226, 226))
end
end
end
ChatService.SpeakerAdded:Connect(speakerAdded)
for _, speaker in ipairs(ChatService:GetSpeakerList()) do
speakerAdded(speaker)
end
I hope this feedback will prove useful towards improving the way you work with the chat system.