Problem with !togglechattag Command

Ok so got a little problem
I want to make it when someone uses the command !togglechattag

Only thing is i know how to make it with /e anyone know how to change that to a normal command but that when you use the command it will also disapear in the chat itself

Tried to figure out where the problem is but couldn’t find t myself Anyone can help and fix the problem

Note:

Code:

local ServerScriptService = game:GetService("ServerScriptService")

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

local GroupID = 8514400 -- Replace 16885155 with your actual Group ID

local function GetGroupId(player)
   local success, result = pcall(function()
   	return player:GetRankInGroup(GroupID)
   end)
   return success and result or nil
end

local GroupTags = {
   [255] = {
   	ChatColor = Color3.fromRGB(255, 0, 0), -- Player Message Color 
   	NameColor = Color3.fromRGB(255, 0, 0), -- Player's Name Color
   	Font = Enum.Font.SourceSansBold, -- 
   	TextSize = 18, -- Size of the Tag PlayerName and Message
   	Tags = { -- Starts the Tags List (chat tags will be contained in this table)
   		{ -- Starts tag 1
   			TagText = "F", -- Change this to the word(s) that you want in the Chat Tag
   			TagColor = Color3.fromRGB(255, 0, 0) -- Change this to the Color you want the Chat Tag to appear in
   		}, -- Ends tag 1
   	}, -- Ends Tags List
   }, -- Ends RoleID

   [254] = {
   	ChatColor = Color3.fromRGB(151, 0, 0), -- Player Message Color 
   	NameColor = Color3.fromRGB(151, 0, 0), -- Player's Name Color
   	Font = Enum.Font.SourceSansBold, -- 
   	TextSize = 18, -- Size of the Tag PlayerName and Message
   	Tags = { -- Starts the Tags List (chat tags will be contained in this table)
   		{ -- Starts tag 1
   			TagText = "HR", -- Change this to the word(s) that you want in the Chat Tag
   			TagColor = Color3.fromRGB(255, 215, 0) -- Change this to the Color you want the Chat Tag to appear in
   		}, -- Ends tag 1
   	}, -- Ends Tags List
   }, -- Ends RoleID

   [244] = {
   	ChatColor = Color3.fromRGB(37, 0, 85), -- Player Message Color 
   	NameColor = Color3.fromRGB(37, 0, 85), -- Player's Name Color
   	Font = Enum.Font.SourceSansBold, -- 
   	TextSize = 18, -- Size of the Tag PlayerName and Message
   	Tags = { -- Starts the Tags List (chat tags will be contained in this table)
   		{ -- Starts tag 1
   			TagText = "DD", -- Change this to the word(s) that you want in the Chat Tag
   			TagColor = Color3.fromRGB(255, 215, 0) -- Change this to the Color you want the Chat Tag to appear in
   		}, -- Ends tag 1
   	}, -- Ends Tags List
   }, -- Ends RoleID

   [243] = {
   	ChatColor = Color3.fromRGB(255, 255, 255), -- Player Message Color 
   	NameColor = Color3.fromRGB(0, 1, 109), -- Player's Name Color
   	Font = Enum.Font.SourceSansBold, -- 
   	TextSize = 18, -- Size of the Tag PlayerName and Message
   	Tags = { -- Starts the Tags List (chat tags will be contained in this table)
   		{ -- Starts tag 1
   			TagText = "MD", -- Change this to the word(s) that you want in the Chat Tag
   			TagColor = Color3.fromRGB(0, 1, 109) -- Change this to the Color you want the Chat Tag to appear in
   		}, -- Ends tag 1
   	}, -- Ends Tags List
   }, -- Ends RoleID

   [242] = {
   	ChatColor = Color3.fromRGB(255, 255, 255), -- Player Message Color 
   	NameColor = Color3.fromRGB(0, 1, 182), -- Player's Name Color
   	Font = Enum.Font.SourceSansBold, -- 
   	TextSize = 18, -- Size of the Tag PlayerName and Message
   	Tags = { -- Starts the Tags List (chat tags will be contained in this table)
   		{ -- Starts tag 1
   			TagText = "ED", -- Change this to the word(s) that you want in the Chat Tag
   			TagColor = Color3.fromRGB(0, 1, 182) -- Change this to the Color you want the Chat Tag to appear in
   		}, -- Ends tag 1
   	}, -- Ends Tags List
   }, -- Ends RoleID

   [241] = {
   	ChatColor = Color3.fromRGB(255, 255, 255), -- Player Message Color 
   	NameColor = Color3.fromRGB(0, 25, 255), -- Player's Name Color
   	Font = Enum.Font.SourceSansBold, -- 
   	TextSize = 18, -- Size of the Tag PlayerName and Message
   	Tags = { -- Starts the Tags List (chat tags will be contained in this table)
   		{ -- Starts tag 1
   			TagText = "D", -- Change this to the word(s) that you want in the Chat Tag
   			TagColor = Color3.fromRGB(0, 25, 255) -- Change this to the Color you want the Chat Tag to appear in
   		}, -- Ends tag 1
   	}, -- Ends Tags List
   }, -- Ends RoleID

   [240] = {
   	ChatColor = Color3.fromRGB(255, 255, 255), -- Player Message Color 
   	NameColor = Color3.fromRGB(0, 67, 255), -- Player's Name Color
   	Font = Enum.Font.SourceSansBold, -- 
   	TextSize = 18, -- Size of the Tag PlayerName and Message
   	Tags = { -- Starts the Tags List (chat tags will be contained in this table)
   		{ -- Starts tag 1
   			TagText = "TD", -- Change this to the word(s) that you want in the Chat Tag
   			TagColor = Color3.fromRGB(0, 67, 255) -- Change this to the Color you want the Chat Tag to appear in
   		}, -- Ends tag 1
   	}, -- Ends Tags List
   }, -- Ends RoleID

   [226] = {
   	ChatColor = Color3.fromRGB(255, 255, 255), -- Player Message Color 
   	NameColor = Color3.fromRGB(116, 0, 92), -- Player's Name Color
   	Font = Enum.Font.SourceSansBold, -- 
   	TextSize = 18, -- Size of the Tag PlayerName and Message
   	Tags = { -- Starts the Tags List (chat tags will be contained in this table)
   		{ -- Starts tag 1
   			TagText = "ML", -- Change this to the word(s) that you want in the Chat Tag
   			TagColor = Color3.fromRGB(116, 0, 92) -- Change this to the Color you want the Chat Tag to appear in
   		}, -- Ends tag 1
   	}, -- Ends Tags List
   }, -- Ends RoleID

   [225] = {
   	ChatColor = Color3.fromRGB(255, 255, 255), -- Player Message Color 
   	NameColor = Color3.fromRGB(91, 0, 0), -- Player's Name Color
   	Font = Enum.Font.SourceSansBold, -- 
   	TextSize = 18, -- Size of the Tag PlayerName and Message
   	Tags = { -- Starts the Tags List (chat tags will be contained in this table)
   		{ -- Starts tag 1
   			TagText = "SA", -- Change this to the word(s) that you want in the Chat Tag
   			TagColor = Color3.fromRGB(91, 0, 0) -- Change this to the Color you want the Chat Tag to appear in
   		}, -- Ends tag 1
   	}, -- Ends Tags List
   }, -- Ends RoleID

   [224] = {
   	ChatColor = Color3.fromRGB(255, 255, 255), -- Player Message Color 
   	NameColor = Color3.fromRGB(124, 0, 0), -- Player's Name Color
   	Font = Enum.Font.SourceSansBold, -- 
   	TextSize = 18, -- Size of the Tag PlayerName and Message
   	Tags = { -- Starts the Tags List (chat tags will be contained in this table)
   		{ -- Starts tag 1
   			TagText = "A", -- Change this to the word(s) that you want in the Chat Tag
   			TagColor = Color3.fromRGB(124, 0, 0) -- Change this to the Color you want the Chat Tag to appear in
   		}, -- Ends tag 1
   	}, -- Ends Tags List
   }, -- Ends RoleID

   [223] = {
   	ChatColor = Color3.fromRGB(255, 255, 255), -- Player Message Color 
   	NameColor = Color3.fromRGB(189, 12, 0), -- Player's Name Color
   	Font = Enum.Font.SourceSansBold, -- 
   	TextSize = 18, -- Size of the Tag PlayerName and Message
   	Tags = { -- Starts the Tags List (chat tags will be contained in this table)
   		{ -- Starts tag 1
   			TagText = "IA", -- Change this to the word(s) that you want in the Chat Tag
   			TagColor = Color3.fromRGB(189, 12, 0) -- Change this to the Color you want the Chat Tag to appear in
   		}, -- Ends tag 1
   	}, -- Ends Tags List
   }, -- Ends RoleID
   [222] = {
   	ChatColor = Color3.fromRGB(255, 255, 255), -- Player Message Color 
   	NameColor = Color3.fromRGB(0, 44, 77), -- Player's Name Color
   	Font = Enum.Font.SourceSansBold, -- 
   	TextSize = 18, -- Size of the Tag PlayerName and Message
   	Tags = { -- Starts the Tags List (chat tags will be contained in this table)
   		{ -- Starts tag 1
   			TagText = "SM", -- Change this to the word(s) that you want in the Chat Tag
   			TagColor = Color3.fromRGB(0, 44, 77) -- Change this to the Color you want the Chat Tag to appear in
   		}, -- Ends tag 1
   	}, -- Ends Tags List
   }, -- Ends RoleID
   [221] = {
   	ChatColor = Color3.fromRGB(255, 255, 255), -- Player Message Color 
   	NameColor = Color3.fromRGB(0, 65, 111), -- Player's Name Color
   	Font = Enum.Font.SourceSansBold, -- 
   	TextSize = 18, -- Size of the Tag PlayerName and Message
   	Tags = { -- Starts the Tags List (chat tags will be contained in this table)
   		{ -- Starts tag 1
   			TagText = "M", -- Change this to the word(s) that you want in the Chat Tag
   			TagColor = Color3.fromRGB(0, 65, 111) -- Change this to the Color you want the Chat Tag to appear in
   		}, -- Ends tag 1
   	}, -- Ends Tags List
   }, -- Ends RoleID

   [220] = {
   	ChatColor = Color3.fromRGB(255, 255, 255), -- Player Message Color 
   	NameColor = Color3.fromRGB(0, 105, 180), -- Player's Name Color
   	Font = Enum.Font.SourceSansBold, -- 
   	TextSize = 18, -- Size of the Tag PlayerName and Message
   	Tags = { -- Starts the Tags List (chat tags will be contained in this table)
   		{ -- Starts tag 1
   			TagText = "TM", -- Change this to the word(s) that you want in the Chat Tag
   			TagColor = Color3.fromRGB(0, 105, 180) -- Change this to the Color you want the Chat Tag to appear in
   		}, -- Ends tag 1
   	}, -- Ends Tags List
   }, -- Ends RoleID

   [4] = {
   	ChatColor = Color3.fromRGB(255, 255, 255), -- Player Message Color 
   	NameColor = Color3.fromRGB(0, 24, 255), -- Player's Name Color
   	Font = Enum.Font.SourceSansBold, -- 
   	TextSize = 18, -- Size of the Tag PlayerName and Message
   	Tags = { -- Starts the Tags List (chat tags will be contained in this table)
   		{ -- Starts tag 1
   			TagText = "TM", -- Change this to the word(s) that you want in the Chat Tag
   			TagColor = Color3.fromRGB(0, 24, 255) -- Change this to the Color you want the Chat Tag to appear in
   		}, -- Ends tag 1
   	}, -- Ends Tags List
   }, -- Ends RoleID

   [6] = {
   	ChatColor = Color3.fromRGB(255, 255, 255), -- Player Message Color 
   	NameColor = Color3.fromRGB(255, 170, 0), -- Player's Name Color
   	Font = Enum.Font.SourceSansBold, -- 
   	TextSize = 18, -- Size of the Tag PlayerName and Message
   	Tags = { -- Starts the Tags List (chat tags will be contained in this table)
   		{ -- Starts tag 1
   			TagText = "VIP", -- Change this to the word(s) that you want in the Chat Tag
   			TagColor = Color3.fromRGB(255, 170, 0) -- Change this to the Color you want the Chat Tag to appear in
   		}, -- Ends tag 1
   	}, -- Ends Tags List
   }, -- Ends RoleID

   [5] = {
   	ChatColor = Color3.fromRGB(255, 255, 255), -- Player Message Color 
   	NameColor = Color3.fromRGB(2, 105, 105), -- Player's Name Color
   	Font = Enum.Font.SourceSansBold, -- 
   	TextSize = 18, -- Size of the Tag PlayerName and Message
   	Tags = { -- Starts the Tags List (chat tags will be contained in this table)
   		{ -- Starts tag 1
   			TagText = "PT", -- Change this to the word(s) that you want in the Chat Tag
   			TagColor = Color3.fromRGB(2, 105, 105) -- Change this to the Color you want the Chat Tag to appear in
   		}, -- Ends tag 1
   	}, -- Ends Tags List
   }, -- Ends RoleID

   [4] = {
   	ChatColor = Color3.fromRGB(255, 255, 255), -- Player Message Color 
   	NameColor = Color3.fromRGB(0, 148, 148), -- Player's Name Color
   	Font = Enum.Font.SourceSansBold, -- 
   	TextSize = 18, -- Size of the Tag PlayerName and Message
   	Tags = { -- Starts the Tags List (chat tags will be contained in this table)
   		{ -- Starts tag 1
   			TagText = "T", -- Change this to the word(s) that you want in the Chat Tag
   			TagColor = Color3.fromRGB(0, 148, 148) -- Change this to the Color you want the Chat Tag to appear in
   		}, -- Ends tag 1
   	}, -- Ends Tags List
   }, -- Ends RoleID

   [3] = {
   	ChatColor = Color3.fromRGB(255, 255, 255), -- Player Message Color 
   	NameColor = Color3.fromRGB(43, 255, 0), -- Player's Name Color
   	Font = Enum.Font.SourceSansBold, -- 
   	TextSize = 18, -- Size of the Tag PlayerName and Message
   	Tags = { -- Starts the Tags List (chat tags will be contained in this table)
   		{ -- Starts tag 1
   			TagText = "M", -- Change this to the word(s) that you want in the Chat Tag
   			TagColor = Color3.fromRGB(43, 255, 0) -- Change this to the Color you want the Chat Tag to appear in
   		}, -- Ends tag 1
   	}, -- Ends Tags List
   }, -- Ends RoleID

   [2] = {
   	ChatColor = Color3.fromRGB(255, 255, 255), -- Player Message Color 
   	NameColor = Color3.fromRGB(255, 0, 0), -- Player's Name Color
   	Font = Enum.Font.SourceSansBold, -- 
   	TextSize = 18, -- Size of the Tag PlayerName and Message
   	Tags = { -- Starts the Tags List (chat tags will be contained in this table)
   		{ -- Starts tag 1
   			TagText = "D", -- Change this to the word(s) that you want in the Chat Tag
   			TagColor = Color3.fromRGB(255, 0, 0) -- Change this to the Color you want the Chat Tag to appear in
   		}, -- Ends tag 1
   	}, -- Ends Tags List
   }, -- Ends RoleID

   [1] = {
   	ChatColor = Color3.fromRGB(255, 255, 255), -- Player Message Color 
   	NameColor = Color3.fromRGB(255, 55, 0), -- Player's Name Color
   	Font = Enum.Font.SourceSansBold, -- 
   	TextSize = 18, -- Size of the Tag PlayerName and Message
   	Tags = { -- Starts the Tags List (chat tags will be contained in this table)
   		{ -- Starts tag 1
   			TagText = "AP", -- Change this to the word(s) that you want in the Chat Tag
   			TagColor = Color3.fromRGB(255, 55, 0) -- Change this to the Color you want the Chat Tag to appear in
   		}, -- Ends tag 1
   	}, -- Ends Tags List
   }, -- Ends RoleID
} -- Ends GroupTags

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

   if player then
   	local groupRankID = GetGroupId(player)
   	if groupRankID then
   		local extraData = GroupTags[groupRankID]
   		if extraData then
   			for key, value in pairs(extraData) do
   				speaker:SetExtraData(key, value)
   			end
   		end
   	end
   end
end

ChatService.SpeakerAdded:Connect(handleSpeaker)
for _, speakerName in pairs(ChatService:GetSpeakerList()) do
   handleSpeaker(speakerName)
end

local toggles = {}

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

   if player then
   	local groupRankID = GetGroupId(player)
   	if groupRankID and GroupTags[groupRankID] then
   		if not toggles[groupRankID] then
   			toggles[groupRankID] = true
   			speaker:SetExtraData('Tags', {})
   		else
   			toggles[groupRankID] = false
   			speaker:SetExtraData('Tags', GroupTags[groupRankID].Tags)
   		end
   	end
   end
end

game.Players.PlayerAdded:Connect(function(plr)
   plr.Chatted:Connect(function(msg)
   	if msg:lower() == "/e !togglechattag" then
   		local groupRankID = GetGroupId(plr)
   		if groupRankID >= 220 then -- Replace 255 with the required Rank ID
   			toggle(plr.Name)
   		else
   	    print("Player is not High Enought Rank To use /e !ToggleChatTag")
   		end
   	end
   end)
end)

game.Players.PlayerRemoving:Connect(function(plr)
   local groupRankID = GetGroupId(plr)
   if groupRankID and toggles[groupRankID] then
   	toggles[groupRankID] = nil
   end
end)

for i, plr in pairs(game.Players:GetPlayers()) do
   plr.Chatted:Connect(function(msg)
   	if msg:lower() == "/e !togglechattag" then
   		local groupRankID = GetGroupId(plr)
   		if groupRankID >= 220 then -- Replace 255 with the required Rank ID
   			toggle(plr.Name)
   		else
   			print("Player is not High Enought Rank To use /e !ToggleChatTag")
   		end
   	end
   end)
end

Are you using LegacyChat or Robloxs new ChatService?
I am aware there are many issues with roblox new chat not responding to .Chatted events.

So Far i know i use the new chat system where you see that?

Okay not answering you directly, apologies, but this event may be useful for you to try use instead:

I am not very good at the new text chat service so you will have to rely on others below for assistance, that is my one suggestion if .Chatted does not work, elsewise experiment getting .Chatted on a local script and passing that along using a remote event.

The ChatServiceRunner doesn’t work in the new chat system with modern ui aka TextChatService sadly.

Ah thats why Gone look after t to change