Filtering Textboxes in Studio

Myself personally I haven’t seen much on filtering textboxes that don’t give an in-depth answer of how to do it. Anyways here’s how you can do it. Hope this helps anyone.

  1. Make a Remote Event in ReplicatedStorage. **Name it ‘FilterRE’ (Of course you can change it just change the paths in the code.) **

  2. Make a Local Script as a child in your TextBox

  3. Make a Server Script in ServerScriptService

Server Script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TextService = game:GetService("TextService")
local remote = ReplicatedStorage:WaitForChild("FilterRE")

remote.OnServerEvent:Connect(function(player, message)
    if type(message) ~= "string" or message == "" then
        remote:FireClient(player, "")
        return
    end

    local success, filteredText
    pcall(function()
        local filteredTextObject = TextService:FilterStringAsync(message, player.UserId)
        filteredText = filteredTextObject:GetChatForUserAsync(player.UserId)
    end)

    if not success or not filteredText then
        filteredText = "[Message failed to filter]"
    end

    remote:FireClient(player, filteredText)
end)

Local Script:

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

local player = Players.LocalPlayer
local remote = ReplicatedStorage:WaitForChild("FilterRE")

script.Parent.FocusLost:Connect(function(enterPressed)
    if enterPressed then
        local message = script.Parent.Text

        if message and message ~= "" then
            remote:FireServer(message)
        end
    end
end)

remote.OnClientEvent:Connect(function(filteredMessage)
    if filteredMessage then
        script.Parent.Text = filteredMessage
    end
end)

1 Like

You should be using a RemoteFunction here! It seems that’s the behavior you’re trying to replicate using RemoteEvents.


I believe it will not be a good idea to filter TextBoxes, but rather a TextLabel near it, showing you a filtered version on the side. If the user is typing new input and the server decides to set the text, it won’t be a nice user experience. I’ve seem games do this, but not all games. The issues will be more noticeable with more latency.

2 Likes

I think you need a PCall. Otherwise you might get errors.

2 Likes