Why won't this chat tag script work?

I am trying to make a chat tag script using the new TextChatService for my game: Noob Minigames. I made this script using the Roblox Documentation site and edited it to work with my game. I have no idea why, but my script won’t work, and I have been trying to figure it out for days so I decided to post this to see if anyone could help. (P.S This is my first ever post asking for help.)

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

TextChatService.OnIncomingMessage = function(message: TextChatMessage)
	local properties = Instance.new("TextChatMessageProperties")
	local OwnerPlayerId = 792651524
	local TesterPlayerId = 905724063
	local BugFinderPlayerId = 216745876
	local ArtistPlayerId = 423038282
	local ThinkerPlayerId = 701646147
	local BuilderPlayerId = 1667911408
	local BugFinder2PlayerId = 1699249602
	local Builder2PlayerId = 593063138
	local GroupId = 9477217

	if message.TextSource then
		local player = Players:GetPlayerByUserId(message.TextSource.UserId)
		if player:IsInGroup(GroupId) then
			local Role = player:GetRoleInGroup(GroupId)
			if Role == "Guest" then
				properties.PrefixText = message.PrefixText
			elseif Role == "BigHead" then
				properties.PrefixText = "<font color='#00ffff'>[Fan]</font> " .. message.PrefixText
			elseif Role == "BiggerHead" then
				properties.PrefixText = "<font color='#ff0000'>[Superfan]</font> " .. message.PrefixText
			elseif Role == "Moderator" then
				properties.PrefixText = "<font color='#ffff00'>[Moderator]</font> " .. message.PrefixText
			end
		end
		if player.UserId == OwnerPlayerId then
			properties.PrefixText = "<font color='#ffaa00'>[πŸ‘‘ Owner πŸ‘‘]</font> " .. message.PrefixText
		elseif player.UserId == TesterPlayerId then
			properties.PrefixText = "<font color='#ffaaff'>[πŸ™ƒ Tester πŸ™ƒ]</font> " .. message.PrefixText
		elseif player.UserId == BugFinderPlayerId then
			properties.PrefixText = "<font color='#55aa00'>[🐝 Bug Finder 🐝]</font> " .. message.PrefixText
		elseif player.UserId == ArtistPlayerId then
			properties.PrefixText = "<font color='#00aaff'>[🎨 Artist 🎨]</font> " .. message.PrefixText
		elseif player.UserId == ThinkerPlayerId then
			properties.PrefixText = "<font color='#00aaff'>[πŸ€” Thinker πŸ€”]</font> " .. message.PrefixText
		elseif player.UserId == BuilderPlayerId then
			properties.PrefixText = "<font color='#b19cd9'>[πŸ”¨ Builder πŸ”¨]</font> " .. message.PrefixText
		elseif player.UserId == BugFinder2PlayerId then
			properties.PrefixText = "<font color='#55aa00'>[πŸ¦‹ Bug Finder πŸ¦‹]</font> " .. message.PrefixText
		elseif player.UserId == Builder2PlayerId then
			properties.PrefixText = "<font color='#ff0000'>[πŸ”§ Builder πŸ”§]</font> " .. message.PrefixText
		end
	end

	return properties
end

I placed this LocalScript in StarterPlayer>StarterPlayerScripts and it wouldn’t work as well as ServerScriptService.

2 Likes

I may be incorrect but does the chat service only work in server scripts? I may be wrong because I have not used the chat service recently but I swear I remember something like that.

3 Likes

Just tried putting it in ServerScriptService, still wouldn’t work.

1 Like

The original script seemed to work for me, but I would recommend against fetching for group membership information as often as this. What you can do is when a player joins your game, you can fetch for their group membership information once and store it as an attribute of that player. That allows your chat callback and anything else in your game to reference this information at a moments notice.

You can do this by creating a Script in ServerScriptService:

local Players = game:GetService("Players")

local GROUP_ID = 9477217

Players.PlayerAdded:Connect(function(player)
	local isInGroup = player:IsInGroup(GROUP_ID)
	player:SetAttribute("isInGroup", isInGroup)
	
	if isInGroup then
		local groupRole = player:GetRoleInGroup(GROUP_ID)
		if groupRole then
			player:SetAttribute("groupRole", groupRole)
		end
	end
end)

This has the extra benefit of giving you a little more information about whats going on in the Studio properties window when you select a Player:

Then you can modify your original LocalScript in StarterPlayerScripts:

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

TextChatService.OnIncomingMessage = function(message: TextChatMessage)
	local properties = Instance.new("TextChatMessageProperties")
	local OwnerPlayerId = 792651524
	local TesterPlayerId = 905724063
	local BugFinderPlayerId = 216745876
	local ArtistPlayerId = 423038282
	local ThinkerPlayerId = 701646147
	local BuilderPlayerId = 1667911408
	local BugFinder2PlayerId = 1699249602
	local Builder2PlayerId = 593063138

	if message.TextSource then
		local player = Players:GetPlayerByUserId(message.TextSource.UserId)
		if player:GetAttribute("isInGroup") then
			local Role = player:GetAttribute("groupRole")
			if Role == "Guest" then
				properties.PrefixText = message.PrefixText
			elseif Role == "BigHead" then
				properties.PrefixText = "<font color='#00ffff'>[Fan]</font> " .. message.PrefixText
			elseif Role == "BiggerHead" then
				properties.PrefixText = "<font color='#ff0000'>[Superfan]</font> " .. message.PrefixText
			elseif Role == "Moderator" then
				properties.PrefixText = "<font color='#ffff00'>[Moderator]</font> " .. message.PrefixText
			end
		end
		
		if player.UserId == OwnerPlayerId then
			properties.PrefixText = "<font color='#ffaa00'>[πŸ‘‘ Owner πŸ‘‘]</font> " .. message.PrefixText
		elseif player.UserId == TesterPlayerId then
			properties.PrefixText = "<font color='#ffaaff'>[πŸ™ƒ Tester πŸ™ƒ]</font> " .. message.PrefixText
		elseif player.UserId == BugFinderPlayerId then
			properties.PrefixText = "<font color='#55aa00'>[🐝 Bug Finder 🐝]</font> " .. message.PrefixText
		elseif player.UserId == ArtistPlayerId then
			properties.PrefixText = "<font color='#00aaff'>[🎨 Artist 🎨]</font> " .. message.PrefixText
		elseif player.UserId == ThinkerPlayerId then
			properties.PrefixText = "<font color='#00aaff'>[πŸ€” Thinker πŸ€”]</font> " .. message.PrefixText
		elseif player.UserId == BuilderPlayerId then
			properties.PrefixText = "<font color='#b19cd9'>[πŸ”¨ Builder πŸ”¨]</font> " .. message.PrefixText
		elseif player.UserId == BugFinder2PlayerId then
			properties.PrefixText = "<font color='#55aa00'>[πŸ¦‹ Bug Finder πŸ¦‹]</font> " .. message.PrefixText
		elseif player.UserId == Builder2PlayerId then
			properties.PrefixText = "<font color='#ff0000'>[πŸ”§ Builder πŸ”§]</font> " .. message.PrefixText
		end
	end

	return properties
end

Does this end up working any better for you? I’m wondering if the fetching of membership data is happening too slowly doing it last-minute the other way.

2 Likes

Thanks again so much for fixing this, spent 4 hours on it before you helped :slight_smile:

1 Like

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