because I just use table.insert and table.remove and that works just as well but I’m not sure if that works because I used to use :SetExtraData()
.
Yes, in order to give player a chat tag, change their name/message text colour you have to use it.
so is using table.insert and unreliable way?
You don’t use table.insert to give player chat tags, wdym?
Can you give me an example of doing that?
here is code that I use
local ChatService = require(game.ServerScriptService:WaitForChild("ChatServiceRunner").ChatService)
game.ReplicatedStorage.ChangeTags.OnServerEvent:connect(function(player, Text, ColorTag, Adding)
local Speaker = ChatService:GetSpeaker(player.Name)
if Adding == true then
table.insert(Speaker:GetExtraData("Tags"), #Speaker:GetExtraData("Tags") +1, {TagText = Text, TagColor = ColorTag})
elseif Adding == false then
for i,Tag in pairs(Speaker:GetExtraData("Tags")) do
if Tag.TagText == Text and Tag.TagColor == ColorTag then
table.remove(Speaker:GetExtraData("Tags"), i)
end
end
end
end)
Oh, :GetExtraData(“Tags”) just returns a table of tags, you aren’t actually using table.insert to give someone a tag. What you are doing is store all the tags in a table and probably use them later.
You should be using Set/GetExtraData because those are the canonically correct ways to be handling player extra data and they make it clear what you are doing in code. Additionally, these functions may have internal behaviours you may not know of: for example, SetExtraData fires an internal event for ChatSpeaker objects that table.insert wouldn’t.
(This repository overall is outdated but the content linked here is still relevant.)
so are you saying I can use :GetExtraData()
and not have to use :SetExtraData()
at all?
:GetExtraData() is used to get info about tags while :SetExtraData() is used to add tags
isnt it :GetExtraData(“Tags”) just a table of the tags the player currently has? so if i use table.insert its just adding the tag data (or whatever you call it) to the table of tags?
I’m actually saying the opposite. GetExtraData just returns whatever’s at the indice of the argument you pass, so there’d be no difference between these:
local level = speaker:GetExtraData("Level")
local level = speaker.ExtraData.Level
For SetExtraData though, it also fires an event internally which detects for changes to ExtraData keys which is then communicated to other systems to act when there’s new extra data in place. So ideally you should use the method instead of manually doing this work.
Just use Set/Get. Why are you adverse to using them?
I was using :SetExtraData() before but since I have a GUI where you can edit your chat tags and since there is multiple tags I dont want to have to use SetExtraData() for every possible tag combination, because if i learned from it, SetExtraData replaces the current tags (sorry for taking so long) besides it works just fine i dont even know why i asked this, im sorry
Don’t be sorry. Learning is good.
For a system like that, ideally you’ll want to change your strategy there. Use a separate table to store your tags, so use insert or remove based on if a user selects a new tag.
local selectedTags = {}
table.insert(selectedTags, {tagContent})
Then from there, all you’d need to do is use SetExtraData with Tags for the key and selectedTable as the value at any time you want tags to update. This way, tags don’t get overwritten. Your real tags table is selectedTable and any calls to SetExtraData are just setting it to the table you’re updating.
You can either have a wrapper function that inserts a tag and calls SetExtraData if you want instant updates, or just call SetExtraData if you have a “confirm tags” button.
Makes sense or would you like an example to more clearly show what I mean?
yeah, can you give me an example, please?
Absolutely!
local selectedTags = {}
-- For a confirm button approach, simply add tags to a table
table.insert(selectedTags, {TagText = "foo"})
-- Or remove them if you like
table.remove(selectedTags, 1)
-- And set them down
speaker:SetExtraData("Tags", selectedTags)
-- If you want them to instantly update, use wrappers!
local function addTag(tagText, tagColor)
table.insert(selectedTags, {TagText = tagText, TagColor = tagColor})
speaker:SetExtraData("Tags", selectedTags)
end
-- And to remove them:
local function removeTag(position)
table.remove(selectedTags, position)
speaker:SetExtraData("Tags", selectedTags)
end
Ah, an important thing: remember that working with extra data is (or should be) handled by the server. ChatSpeaker APIs are server-side. Remotes will be necessary.
yeah I’m using remotes so don’t worry. I have also checkboxes for selected tags that make it update instantly, so if you spam a button, will that overflow anything? but I’ll have to try it
I have four different buttons and each have a unique local script inside, how will i get a table across all four?
You could possibly use a RemoteFunction where you invoke it from the client yet return the table from the server.
In that case, I usually only use one LocalScript to control my interface. I will never normally have more than one LocalScript working on a Gui and if I need more code working on a Gui, I’ll split it up into ModuleScripts.
hi i know this is an old post but is there anyway you could give me guidance on how i would change the players name colour?