Cant filter text

Writing a name system and i need to filter text becuz its mandatory been stuck on this for a while now everything returns true for some reason

local script:

First_Name_Button.FocusLost:Connect(function()
	if First_Name_Button.Text ~= nil then
		local result = FilterTextEvent:InvokeServer(First_Name_Button.Text)
		
		print(result)
		
	end
end)

server script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TextService = game:GetService("TextService")
local FilterTextEvent = ReplicatedStorage.RemoteEvents.FilterText

function OnInvoke(player,text)
	
	local filter
	local success, errormsg = pcall(function()
		filter = TextService:FilterStringAsync(text, player.UserId)
	end)
	
	if not success then
		warn("Error filtering text:", text, ":", errormsg)
		-- Put code here to handle filter failure
		return false
	else
		return true
	end
end

FilterTextEvent.OnServerInvoke = OnInvoke

Have you tried this script using the actual Roblox Player? Filtering does not work in Studio.

1 Like

ye just tried same result

EDIT There’s also another problem with your code. I’ve edited this post to include another solution.

Your rewritten code

First_Name_Button.FocusLost:Connect(function()
	if First_Name_Button.Text ~= nil then
		local success, filter = FilterTextEvent:InvokeServer(First_Name_Button.Text)
		
		print(success, filter)
	end
end)
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TextService = game:GetService("TextService")
local FilterTextEvent = ReplicatedStorage.RemoteEvents.FilterText

function OnInvoke(player,text)
	
	local filter
	local success, errormsg = pcall(function()
		filter = (TextService:FilterStringAsync(text, player.UserId)):GetNonChatStringForUserAsync(player.UserId)
	end)

	if not success then
		warn("Error filtering text:", text, ":", errormsg)
		-- Put code here to handle filter failure
		return false
	else
		return true, filter
	end
end

FilterTextEvent.OnServerInvoke = OnInvoke

Your initial problems

  1. TextService:FilterStringAsync() returns an Instance, and not the filtered text that you’re likely expecting, hence why the function always returns true. Use TextFilterResult:GetNonChatStringForUserAsync() to use on the filter property.
  2. You’re returning only a bool that identifies whether or not the filter was successful, not the actual filtered text. If you want the function to return the filtered text, then return 2 parameters. For example, on the server:
	if not success then
		warn("Error filtering text:", text, ":", errormsg)
		-- Put code here to handle filter failure
		return false
	else
		return true, filter
	end

Then, on the client:

First_Name_Button.FocusLost:Connect(function()
	if First_Name_Button.Text ~= nil then
		local success, result = FilterTextEvent:InvokeServer(First_Name_Button.Text)
		
		print(success, result)
	end
end)
1 Like

There’s maybe an error here because I can’t really see in the console the result of the text

very goated.

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