/tog to toggle chattag

Hi! I am trying to make a /tog system so admins can toggle their chattag on and off.


but I got that error everytime I try to chat after I use /tog.
image

I am using attributes to set the rank.

Attribute Script

local Players = game:GetService("Players")
local groupId = 10618939

Players.PlayerAdded:Connect(function(player)
	local success, isInGroup = pcall(function()
		return player:IsInGroup(groupId)
	end)

	if success and isInGroup then
		local rank = player:GetRankInGroup(groupId)
		player:SetAttribute("GroupRank", rank)
	else
		player:SetAttribute("GroupRank", 0)
	end
end)

Chat Tag Script

local Players = game:GetService("Players")
local TextChatService = game:GetService("TextChatService")

local rankTags = {
	[255] = {TagText = "⭐ | [Owner]", TagColor = Color3.fromRGB(255, 255, 0)},
	[254] = {TagText = "🔥 | [Co-Owner]", TagColor = Color3.fromRGB(255, 165, 0)},
	[253] = {TagText = "🛠️ | [Developer]", TagColor = Color3.fromRGB(0, 255, 0)},
	[100] = {TagText = "⚡ | [Admin]", TagColor = Color3.fromRGB(255, 0, 0)},
}

local toggledPlayers = {}

TextChatService.OnIncomingMessage = function(message)
	local properties = Instance.new("TextChatMessageProperties")
	local textSource = message.TextSource
	local player = Players:GetPlayerByUserId(textSource.UserId)

	if player then
		if message.Text:lower() == "/tog" then
			if toggledPlayers[player.UserId] == true then
				toggledPlayers[player.UserId] = false
			else
				toggledPlayers[player.UserId] = true
			end
		end

		if toggledPlayers[player.UserId] == false then
			properties.PrefixText = nil
		else
			local rank = player:GetAttribute("GroupRank")
			local tagInfo = rankTags[rank]
			if tagInfo then
				local tagText = string.format('<font color="#%02X%02X%02X">%s</font> ', 
					tagInfo.TagColor.R * 255, tagInfo.TagColor.G * 255, tagInfo.TagColor.B * 255, 
					tagInfo.TagText
				)
				properties.PrefixText = tagText .. (message.PrefixText or "")
			end
		end
	end

	return properties
end

Have you tried

properties.PrefixText = ""

in line 28 of the Chat Tag Script. Output says it only accepts a string, not nil, and you set it to nil there…

oh, i’m new to scripting so i didn’t know that it doesnt work.
It fixed the chat problem, but I have a new one.
image
It only toggles on whenever its the /tog message

Hmm, this is odd. Sorry for the late response, I was looking into the problem further. It seems not to be caused by you.

I had just reported a Roblox Studio Bug but this literally seems to be another. Upon further looking into this I have found the problem and a fix for it, but this fix should not be necessary. Studio calls the OnIncomingMessage Callback two times per message. It should only be called once. This is also why you only get the Owner tag when saying /tog, because your function is fired.

Below I have expanded your code to fully erase the problem. I will further look into it and might make another bug report.

local Players = game:GetService("Players")
local TextChatService = game:GetService("TextChatService")

local rankTags = {
	[255] = {TagText = "⭐ | [Owner]", TagColor = Color3.fromRGB(255, 255, 0)},
	[254] = {TagText = "🔥 | [Co-Owner]", TagColor = Color3.fromRGB(255, 165, 0)},
	[253] = {TagText = "🛠️ | [Developer]", TagColor = Color3.fromRGB(0, 255, 0)},
	[100] = {TagText = "⚡ | [Admin]", TagColor = Color3.fromRGB(255, 0, 0)},
}

local toggledPlayers = {}
local oldMsgID

TextChatService.OnIncomingMessage = function(message)
	if oldMsgID == message.MessageId then return end
	oldMsgID = message.MessageId
	local properties = Instance.new("TextChatMessageProperties")
	local textSource = message.TextSource
	local player = Players:GetPlayerByUserId(textSource.UserId)

	if player then
		if message.Text:lower() == "/tog" then
			if toggledPlayers[player.UserId] == true then
				toggledPlayers[player.UserId] = false
			else
				toggledPlayers[player.UserId] = true
			end
		end

		if toggledPlayers[player.UserId] == false then
			properties.PrefixText = ""
		else
			local rank = player:GetAttribute("GroupRank")
			local tagInfo = rankTags[rank]
			if tagInfo then
				local tagText = string.format('<font color="#%02X%02X%02X">%s</font> ', 
					tagInfo.TagColor.R * 255, tagInfo.TagColor.G * 255, tagInfo.TagColor.B * 255, 
					tagInfo.TagText
				)
				properties.PrefixText = tagText .. (message.PrefixText or "")
			end
		end
	end

	return properties
end

Changed: I added a small verification using the unique MessageId identifier to ensure the Callback is only ran once per message. This should fully mitigate your problem.

Edit: This is NOT a Studio bug. This is intended behavior. It is run once from the client and another time for the server. I don’t know why I didn’t think of this, but is sure does make a lot of sense when thinking about it.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.