How do I go about using filter in a TextBox

Hello!

In my game (not public) there is a PlayersCard where players can add their own info such as Nicknames and other things which include text/string. And while reading the article about TextBox (since I am a bit new to using that object) I wondered how can I use the roblox filter to avoid inappropriate usage.

The filter seems complicated so basically I should do something like this:

local TextBox = script.Parent
–Let’s say our player is trying to be funny and writes things that will be censored (f-word and mean thing to say)
TextBox.FocusLost:Connect(function()
–So when the player has done typing
–(goes through the filter)
– so both things will be ####
print(TextBox.Text)
–The there’s other things fired (events)

end)

It’s a quick question but the article seemed confusing to me.
Thanks for reading.

There is a TextService service which contains a function FilterStringAsync().
Example:

local TextService = game:GetService("TextService")
local filteredTextResult = TextService:FilterStringAsync(text, fromPlayerId)

More information in docs

This should work perfectly fine.

  1. Make a remote called FilterText in replicatedstorage.
  2. Make a localscript into the text box.
  3. Make a regular script in server script service.
-- LOCALSCRIPT FOR THE TEXTBOX
script.Parent:GetPropertyChangedSignal("Text"):Connect(function()
if script.Parent.Text ~= "" then
local filteredtext = game.ReplicatedStorage.FilterText:InvokeServer(script.Parent.Text)
if filteredtext then
print(filteredtext)
else return;
end
else return;
end
end)
-- SCRIPT FOR SERVERSCRIPTSERVICE
local textService = game:GetService("TextService")
game.ReplicatedStorage.FilterText.OnServerInvoke:Connect(function(player, text)
local filteredtext = nil;
local success, err = pcall(function()
filteredtext = textService:FilterStringAsync(text, player.UserId)
end)
if success then
return filteredtext
else
return false;
end
end)
4 Likes