Text filtering is god awful

Recently, I’ve been trying to implement text filtering into my game, but roblox is sniping down anything the player says, no matter how harmless. This makes playing the game literally impossible as talking through envelopes is one of the core concepts. I don’t know if I did something wrong with my code, or if this is just roblox being roblox, but this is never an issue in other games, so I’m a bit confused here.

Any help would be appreciated!

local Module = {}

local TextService = game:GetService("TextService")
local Events = game.ReplicatedStorage:WaitForChild("Events")

Module.Setup = function()
	Events.RequestFilteredText.OnServerInvoke = function(player, inputText)
		
		print(inputText)
		
		if typeof(inputText) ~= "string" or inputText == "" then
			return "[Invalid input]"
		end

		local success, filterResult = pcall(function()
			return TextService:FilterStringAsync(inputText, player.UserId)
		end)

		if not success then
			
			warn("Error generating TextFilterResult:", filterResult)
			
			return "[Filter failed]"
		end

		local ok, filteredText = pcall(function()
			
			local finalResult = filterResult:GetNonChatStringForUserAsync(player.UserId)
			
			return finalResult
		end)

		if ok then
			return filteredText
		else
			warn("Error retrieving filtered text:", filteredText)
			
			return "[Filter error]"
		end
	end
end

return Module
1 Like

Avoid filtering text live, filter it once every time the user submits it.

This method should be called once each time a user submits a message. Do not cache the results of this function and re-use them for separate messages. If a user submits the same text multiple times this method must be called again each time the message is sent. If the results are cached and reused spam detection and many forms of context-aware filtering will be broken and potentially put user safety at risk. Games that improperly use cached results may face moderation.

1 Like

So what should I do instead to display what the result will be?

Filter it when the user submits it, don’t make it visible to other players until it is submitted and filtered.

1 Like

As Nowoshire said. Don’t filter the text live. Although instead of immediately submitting the filtered message. I’d add a function to let the player preview the filtered text first before submitting. Optionally you could even add a logic function to see exactly what parts of the original string got filtered and then highlight them in red for the player using RichText.

1 Like

This solution works, although both of you helped. Thank you!

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