Toggle Chat Tags in game (sorry i'm new to this)

I’m trying to figure out a way to say “/tog” in chat and then it’ll toggle the chat tags. Also maybe allow only the people with chat tags to use the command. I don’t know how to do it… I’ve been searching on youtube and on the developer hub for a while, haven’t seen anything that can help…

Heres the script:

local ServerScriptService = game:GetService("ServerScriptService")

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

local SPECIAL_DATA = {
	[890996562] = {--WxlfEclxpse
		ChatColor = BrickColor.new("White").Color,
		Tags = {
			{
				TagText = "Co-Founder",
				TagColor = Color3.new(0.991318, 0.573877, 0.249638),
			},
			
			{
				TagText = "Developer",
				TagColor = Color3.new(0.946151, 0.822858, 0.487465),
			}
			-- Other tags here
		}
		-- Other message formats here
	},
	-- Other user here
	[3634738499] = { --Rat
		ChatColor = BrickColor.new("White").Color,
		Tags = {
			{
				TagText = "Concept Artist",
				TagColor = Color3.new(0.233143, 0.878538, 0.692668),
			},

			{
				TagText = "Content Creator",
				TagColor = Color3.new(0.309743, 1, 0.622644),
			}
			-- Other tags here
		}
		-- Other message formats here
	},
	--Other user here
	[1143428772] = {--Dragon826K
		ChatColor = BrickColor.new("White").Color,
		Tags = {
			{
				TagText = "Head Concept Artist",
				TagColor = Color3.new(0.190127, 0.687053, 0.549599),
			},

			{
				TagText = "Mod",
				TagColor = Color3.new(0.712535, 0.372519, 0.949508),
			}
			-- Other tags here
		}
		-- Other message formats here
	},
}

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

	if player then
		local extraData = SPECIAL_DATA[player.UserId]
		if extraData then
			for key, value in pairs(extraData) do
				speaker:SetExtraData(key, value)
			end
		end
	end
end

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

you can do this to toggle it off:

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

	if player then
		if SPECIAL_DATA[player.UserId] then
			speaker:SetExtraData('Tags', {})
		end
	end
end

setting the data to {} returns it to default. Also, it seems white is the ChatColor for every special tag, so you don’t need to change it back, but you can if you want.

I put this script after the script i pasted above. Do i have to say a certain word for it to toggle? Cause when i speak in chat, it doesn’t do anything.

Well, you probably didn’t add it to the chat commands.

game.Players.PlayerAdded:Connect(function(plr)
	plr.Chatted:Connect(function(msg)
		if msg:lower() == "/tog" then
			toggleoff(plr.Name)
		end
	end)
end)

Hmm… I tried it. It didn’t work, I also checked output in case, and there isn’t any error

Could you add a print into the function?

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

	if player then
		if SPECIAL_DATA[player.UserId] then
			print("Resetting tags")
			speaker:SetExtraData('Tags', {})
		else
			print("Doesn't have special tags")
		end
	else
		print("No player?")
	end
end

I added a print, it didn’t appear in the developer console and output.

That’s also weird, the function should at least be called. Try adding this as well:

for i, plr in pairs(game.Players:GetPlayers()) do
	plr.Chatted:Connect(function(msg)
		if msg:lower() == "/tog" then
			toggleoff(plr.Name)
		end
	end)
end

I’m assuming that the player joins before the PlayerAdded was called.

oo it works! Is there a way for me to turn it on again?

Against TOS anyways. Don’t do it, as it has no impact on gameplay.

Chat tags are against TOS? i don’t think so because i’ve seen a few other games have chat tags as well.

Chat tags aren’t against TOS and nor are custom commands, so I’m not sure where you got this from.

To toggle it, I’ll change the function a bit.

local toggles = {}

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

	if player then
		if SPECIAL_DATA[player.UserId] then
			if not toggles[player.UserId] then
				toggles[player.UserId] = true
				speaker:SetExtraData('Tags', {})
			else
				toggles[player.UserId] = false
				speaker:SetExtraData('Tags', SPECIAL_DATA[player.UserId].Tags)
			end
		end
	end
end

game.Players.PlayerAdded:Connect(function(plr)
	plr.Chatted:Connect(function(msg)
		if msg:lower() == "/tog" then
			toggle(plr.Name)
		end
	end)
end)

game.Players.PlayerRemoving:Connect(function(plr)
	if toggles[plr.UserId] then
		toggles[plr.UserId] = nil
	end
end)

for i, plr in pairs(game.Players:GetPlayers()) do
	plr.Chatted:Connect(function(msg)
		if msg:lower() == "/tog" then
			toggle(plr.Name)
		end
	end)
end

This combined all of the stuff I provided, so replace everything I had provided.

1 Like

Where did you get your information? The OP is referring to chat tags in game, example: [OWNER] [builderman]: Hello!

If you are referring to the chat filter, please read this topic again.

TL;DR: It is not against TOS, plus, why would they make this a thing in LuaU to use chat tags? (if it was against TOS)

1 Like

You could use this to remove all tags off:

speaker:SetExtraData('Tags', nil)

You can set up a .Chatted event for the /tog command; and a for loop to check if the player is whitelisted to use it, if so, use a bool variable to check if it’s toggled or not, then, just readd the tags back.

One more thing! Is there a way for it to not show up in chat?

Not too good at this myself, but here’s a post that could help.