How to Filter Text in a Text Box

Hey everyone! I hope you are having a great day so far! I started making a little game for fun, which I won’t go in depth about because you probably know why, but in my game, the player can type in a text box, and I was wondering how I can filter this text. I want this because obviously I can’t have players typing who knows what, but right now you can type anything and it will work.

Here is a video demonstrating-

If anyone has anything that can help me, I would really appreciate it, and I am sort of new to scripting, so if you could explain it, that would be really helpful, but not necessary! Please do not mention the ui, this is just testing!

Thank you!!!

1 Like

You can use this function to filter text, but you need to do it on the server.

local TextService = game:GetService("TextService")

function FilterInput(message: string, userId: number): string?
	if typeof(message) ~= "string" or message == "" then
		return nil
	end

	local success, filteredText = pcall(function()
		local filterResult = TextService:FilterStringAsync(message, userId)
		return filterResult:GetNonChatStringForBroadcastAsync()
	end)

	if success and filteredText then
		return filteredText
	else
		warn("Text filtering failed:", filteredText)
		return nil
	end
end

As I currently can’t use Roblox Studio I can’t help you further if you have any issues.

2 Likes

Would I have to connect it to the client? And if so do you know how I can go about doing that.

1 Like

You would have to connect it to the client using RemoteEvents or RemoteFunctions.

I’ll post soon another reply with scripts that you can use for it.

2 Likes

Okay, thank you so much for this help, I will keep trying and let you know if I get it!

1 Like

https://create.roblox.com/docs/ui/text-filtering

2 Likes

Server Script:

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

-- Your filter function from before
function FilterInput(message: string, userId: number): string?
	if typeof(message) ~= "string" or message == "" then
		return nil
	end

	local success, filteredText = pcall(function()
		local filterResult = TextService:FilterStringAsync(message, userId)
		return filterResult:GetNonChatStringForBroadcastAsync()
	end)

	if success and filteredText then
		return filteredText
	else
		warn("Text filtering failed:", filteredText)
		return nil
	end
end

-- Create RemoteFunction for clients to call
local remoteFunction = Instance.new("RemoteFunction")
remoteFunction.Name = "RequestFilter"
remoteFunction.Parent = ReplicatedStorage

-- Handle client requests
remoteFunction.OnServerInvoke = function(player, message)
	return FilterInput(message, player.UserId)
end

Local script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RequestFilter = ReplicatedStorage:WaitForChild("RequestFilter")

--[[
	FilterTextOnClient(message: string): string?
	Sends text to the server for filtering.
	Returns the filtered text (safe to display), or nil if filtering failed.
]]
local function FilterTextOnClient(message: string): string?
	if typeof(message) ~= "string" or message == "" then
		warn("FilterTextOnClient: Invalid message")
		return nil
	end

	local success, result = pcall(function()
		return RequestFilter:InvokeServer(message)
	end)

	if success and result then
		return result
	else
		warn("FilterTextOnClient failed:", result)
		return nil
	end
end

Note that this script may contain bugs, and I’m not responsible for consequenses of using this script. I am still positive that this script does not contain bugs and is aligned with Roblox’s requirements.

2 Likes

Thank you I will use these to help me! I will let you know if I have any more problems with this!!!

2 Likes

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