How to check when filtered text is appropriate

I made a text filtering system for a textbox and the way it is set up, I want only the appropriate text from the textbox to be “accepted” if you will, and so then I can do something further.

I’ve played around with my code where I thought I implemented some code that would check whether the text is appropriate or not but it doesn’t seem to work.

I looked on DevForum and found some posts on this issue but they didn’t help me.


This is code from my client script when the player clicks on the button that sends the text to the server to be examined by TextService in the server script.

-- This is an example Lua code block
local fun = game.ReplicatedStorage:WaitForChild("Filter") --Remote Function

local function onClick() -- Function for clicking button
	local message = script.Parent.Parent.NameYourTeamTb.Text --Gets the Textbox's text
	
	local FilteredTxt = fun:InvokeServer(message) --I passed the text into the remote function
		
		
	if FilteredTxt then
		script.Parent.Parent.NameYourTeamTb.Text = FilteredTxt --NameYourTeamTb is the textbox
		
		wait(5)
			
		script.Parent.Parent.NameYourTeamTb.Text = ""
	end
	
	if FilteredTxt == "Allowed" then
		
		print("Text allowed")
	end
end

script.Parent.MouseButton1Click:Connect(onClick)

Here is what the server script looks like. It contains everything that makes the text filtering actually work. The if statement after I declared the pcall function is what I implemented to try checking for if the filtering failed or didn’t work when the text is fine and doesn’t need any filtering. I figured it would have warned me in the console but nothing happened…

local fun = game.ReplicatedStorage:WaitForChild("Filter")
local ts = game:GetService("TextService")

fun.OnServerInvoke = (function(player, textToFilter) --textToFilter is my text from textbox
	
	local success, er = pcall(function()
		local filteredTextResult = ts:FilterStringAsync(textToFilter,player.UserId)
		return filteredTextResult:GetNonChatStringForBroadcastAsync() --Is this line even needed?
	end) 
	
	if not success then
		warn("Error filtering text", er)	
        return "Allowed"
	end
	

	

end)

If someone could help me out with my problem that would be greatly appreciated. Don’t hesitate asking me further questions.

Have you tried only printing er without the if-statement?

Actually no I haven’t. I read your post wrong.

I just tried printing it outside the statement. It showed me in the console the text

So, I assume that you’ve already tried to check if inappropriate text goes through?

Yes that I’ve done. That part is functional. What’s not functional is checking for appropriate text.

I found this old thread regarding text filtering. I am unaware if the case of “not working in studio” is still a thing.

I’m looking at that article. I’m having difficulties getting it to work. I just thought of another possible solution, do you think if I search through the text of the textbox for only their isn’t hashtags(#) then the text is appropriate. I am filling the text box with hashtags anyway when the text is filtered.

I might try that, do you think that idea could work?

Pretty sure text in textboxes aren’t filtered right away. At least I’ve worked a bit with textboxes lately, and it didn’t seem like the case.

Can I ask for what you’re using this for?