Need help on chat tag system based on group ranks

I’ve made this chat tag system that checks for a player’s rank in a group. It worked once, but after I did a few changes, it just… refuses to work again. I did some debugging, and it printed my rank (254), but didn’t give me the chat tags and instead gave me visitor tags (which only appears when you’re not in the group)?? I am beyond confused and I have no idea what may be broken.
I’ve checked the IDs for every group check, all the same.

The debugging I did:

for k, v in pairs(tableofColors) do
	if k==tostring(plr:GetRankInGroup(10405238)) then
		print(k)
		for k2, v2 in pairs(v) do 
			print(k2)
		end
	end
end

Result:
image

Clearly it has the correct table, right?! But it doesn’t give me the tags!

The code:

local Players = game:GetService('Players')
local ServerScriptService = game:GetService('ServerScriptService')

local ChatService = require(ServerScriptService:WaitForChild('ChatServiceRunner'):WaitForChild('ChatService'))

local tableofColors={
	["8"]={NameColor=Color3.fromRGB(0, 187, 255), ChatColor=Color3.fromRGB(237, 255, 174), Tag={TagText="Administration", TagColor=Color3.fromRGB(255, 184, 69)}},

	["9"]={NameColor=Color3.fromRGB(0, 187, 255), ChatColor=Color3.fromRGB(237, 255, 174), Tag={TagText="Developer", TagColor=Color3.fromRGB(255, 73, 73)}},
	["10"]={NameColor=nil, ChatColor=Color3.fromRGB(255, 255, 255), Tag={TagText="Executive", TagColor=Color3.fromRGB(255, 184, 69)}},
	["254"]={NameColor=Color3.fromRGB(0, 187, 255), ChatColor=Color3.fromRGB(237, 255, 174), Tag={TagText="Co-Owner", TagColor=Color3.fromRGB(255, 76, 17)}, IsCo=true},
	["255"]={NameColor=nil, ChatColor=Color3.fromRGB(237, 255, 174), Tag={TagText="Owner", TagColor=Color3.fromRGB(255, 76, 17)}},
}

ChatService.SpeakerAdded:Connect(function(PlayerName)
	local plr=game.Players[PlayerName]
	local Speaker = ChatService:GetSpeaker(PlayerName)
	print(tostring(plr:GetRankInGroup(10405238)))
	if plr:IsInGroup(10405238) and table.find(tableofColors, tostring(plr:GetRankInGroup(10405238))) then
		local t=tableofColors[tostring(plr:GetRankInGroup(10405238))]
		if t.NameColor~=nil then
			Speaker:SetExtraData('NameColor', t.NameColor)
		end
		Speaker:SetExtraData('ChatColor', t.ChatColor)
		Speaker:SetExtraData('Tags', t.Tag)
		if t.IsCo then
			Speaker:SetExtraData('Tags', tableofColors["9"])
		end
		Speaker:SetExtraData("Font", Enum.Font.Code)
	elseif not plr:IsInGroup(10405238) then
		Speaker:SetExtraData('Tags', {{TagText="Visitor", TagColor=Color3.fromRGB(255, 255, 255)}})
	end
end)

Please help, I’m getting desperate!

What’s the current effect of the tag system? People have problems with chat service quite often, searching may help you find an answer.

I know that I answered one last week and simply posted my group tag script I was using.

I fork the chat however so I can customize it and add more stuff to it, so my script was a part of the extradata initializer module that comes with the ChatService fork.

(Yes I see you colbert. lol)

The main problem is that you’ve written tableofColors as a dictionary but treat it as an array. table.find does not work on dictionaries because dictionaries do not have positions. Access the dictionary by the indice instead which would be the rank of the player in the group.

Additionally, there are various minor problems around your code. I strongly recommend looking at the Lua Chat System documentation for some ideas as to what APIs are available. Notably, some issues:

  • ChatSpeakers have a GetPlayer method that will return the player instance if the speaker is for the player. If you only need the player, you should be using this function instead of indexing them in the Players service.

  • GetRankInGroup returns 0 for players not in a group. Save on the amount of web call-based methods you’re calling and just call GetRankInGroup. You can save this as a variable in the function so that you can use it for comparisons and checks later. Additionally you have debug print statements so it makes sense not to repeat code like this.

  • On the point above, your visitor tag can just be for players who have a rank of 0 in your group.

  • Font is not a natively supported ExtraData field. You need to code that in yourself.

  • Strongly recommend not using a lambda for SpeakerAdded. Hold the function separately and connect it to SpeakerAdded, then call it for any existing speakers. This will make sure your code is future proofed against SpeakerAdded being connected later and thus not firing for speakers added before the connection is established.

Putting this all together, we can get a code sample like this:

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

local GROUP_ID = 10405238

local RANK_EXTRA_DATA = {
    [0] = {
        -- No NameColor or ChatColor; do not need to add as nils
        Tag = {TagText="Visitor", TagColor=Color3.fromRGB(255, 255, 255)}
    },
    -- etc
}

local function speakerAdded(speakerName)
    local speaker = ChatService:GetSpeaker(speakerName)
    local player = speaker:GetPlayer()

    -- If the speaker is not for a player, exit out of the function
    if not player then return end

    -- Fetch their group rank
    local playerRank = player:GetRankInGroup(GROUP_ID)

    -- If there's no applicable data set, exit out
    local rankData = RANK_EXTRA_DATA[playerRank]
    if not rankData then return end

    for key, value in pairs(rankData) do
        if key == "Tag" then
            speaker:SetExtraData("Tags", {value.IsCo == true and RANK_EXTRA_DATA["9"] or value})
        else
            speaker:SetExtraData(key, value)
        end
    end
end

ChatService.SpeakerAdded:Connect(speakerAdded)
for _, speaker in ipairs(ChatService:GetSpeakerList()) do
    task.spawn(speakerAdded, speaker)
end

In game, it’ll look like this:

1 Like

It worked! Thank you so much!
One question though, what is a “lambda”?

Refer to the following thread:

(and any additional searches on the internet for further reading)

1 Like