How do you filter text?

wait(5)

local textService = game:GetService("TextService")
local filterThis = "Discord is filtered right? Also 69 and 420 -- just making sure :/"

print(textService:FilterStringAsync(filterThis, 0, 1))

So that obviously doesn’t work. How do you filter text? There aren’t any tutorials and Wiki isn’t helpful in explaining how to use it :frowning:

6 Likes

TextService:FilterStringAsync() does not return a string, you should probably see this:

You should be using it like this:
textService:FilterStringAsync(filterThis, 0, 1):GetChatForUserAsync(0) -- Insert the user ID for the player you want to see the message.

1 Like

Shouldn’t the Chat already be filtered automatically?

2 Likes

It’s for a TextBox that will be used by the script to create a billboard GUI

It didn’t filter the text. Nothing is hash-tagged?

Not sure what’s the problem, it worked for me

Edit: I was using it with chat speakers, it’s probably the cause of this

1 Like

Filtering doesn’t work in Studio, so you’ll want to test it in-game, also if you’re planning to have it publicly visible for other players such as a BillboardGui, you’ll want to use FilterStringForBroadcast, but you can use FilterStringAsync if it’s between players.

Example textbox filter:

local player = game:GetService("Players").LocalPlayer

-- Place this code in a LocalScript inside a TextBox
local textBox = script.Parent
 
local function onFocusLost(enterPressed, inputObject)
	if enterPressed then
		local message = textBox.Text
		local filteredMessage = game:GetService("Chat"):FilterStringForBroadcast(message, player)
		textBox.Text = filteredMessage
	end
end
 
textBox.FocusLost:Connect(onFocusLost)
textBox.Focused:Connect(onFocused)

I would use RemoteEvents however for actual proper filtering (Roblox doesn’t like you using the filter on client).

24 Likes

TextService will filter a string three different ways and then return a TextFilterResult. You need to call a method from that object in order to get the proper string used for filtering, depending on your use case.

local TextService = game:GetService("TextService")

local FILTER_THIS = "Discord is filtered right? Also 69 and 420 -- just making sure :/"

local filterResult = TextService:FilterStringAsync(FILTER_THIS, 0, 1)
local filteredText = filterResult:GetNonChatStringForBroadcastAsync()

print(filteredText)

Filtering cannot be tested in Studio and a valid UserId must be supplied for this to work. You also should be using pcall as FilterStringAsync is a web call and is error-bound, so you need to reject sending a message or queue it if the filter is down.

7 Likes

You can use FilterStringForBroadcast.
Example (message is the Unfiltered message and player is the player who sent the message):

local FilteredMessage = game:GetService("Chat"):FilterStringForBroadcast(message,player)

For more information - Chat | Documentation - Roblox Creator Hub

You can also use the methods above

2 Likes