Filtered text error or success not working?

So I have this naming system where its customized and you can put any name, but I made a filter that always makes it filtered out and an error, im definetly doing something wrong any help?

local plr = game.Players.LocalPlayer

local ChatService = game:GetService("Chat")
 
script.Parent.SubmitButton.MouseButton1Click:Connect(function()
local text = script.Parent.NameType.Text
        local FilteredText = ChatService:FilterStringAsync(text, plr, plr)

        local name = string.sub(FilteredText, 5, string.len(FilteredText))
local TextService = game:GetService("TextService")

local function errorfilter()
script.Parent.BadError.Visible = true
wait(2)
script.Parent.BadError.Visible = false
end

local filteredText = text
local success, errorMessage = pcall(function()
local filteredTextResult = TextService:FilterStringAsync(text)
print("sucess")
end)
if not success then
warn("Error filtering text:", text, ":", errorMessage)
errorfilter()
print("failed")
end
end)

Why are you filtering the text twice? You are calling Chat:FilterStringAsync on the text of the NameType textbox. You are then calling TextService:FilterStringAsync on the same textbox. You don’t even use the result of Chat:FilterStringAsync at all (other than the name variable, which isn’t used after being defined).

Additionally, you can’t filter text on the client. You need to use a remote event or remote function and filter the text on the server.

so how would I go about doing this on server and sending it back to the client,

Read this and this.

Im fully aware of how client to server, etc, works. I just am curious how should, or would, go about this.

If you intend on having the name-changing system change the player’s name and intend on everyone else seeing the new name, you should send the unfiltered text to the server via a remote event. In the server script, filter the text and display it accordingly. In this case the client should not be doing any work other than giving the server the inputted text (and maybe close the UI if that’s what you want?)

Edit: If you want to have the UI also display a success/error message, you should use a remote function rather than a remote event, and have the server send back to the client a message or something of the sort to be displayed.

So I got this, but even when I put something bad it doesnt filter it.

Client:

local plr = game.Players.LocalPlayer

local ChatService = game:GetService("Chat")

local function errorfilter()

script.Parent.BadError.Visible = true

wait(2)

script.Parent.BadError.Visible = false

end

script.Parent.SubmitButton.MouseButton1Click:Connect(function()

local text = script.Parent.NameType.Text

script.Parent.FilterText:FireServer(text)

script.Parent.TextBack.OnClientEvent:Connect(function(plr,textfree)

print("received")

local name = type(textfree)

print("filtered")

local filteredText = text

local success, errorMessage = pcall(function()

print("success")

end)

if not success then

warn("Error filtering text:", text, ":", errorMessage)

errorfilter()

print("failed")

end

end)

end)

Server:

local ChatService = game:GetService("Chat")

script.Parent.FilterText.OnServerEvent:Connect(function(plr,text)

local FilteredText = ChatService:FilterStringAsync(text, plr, plr)

local name = string.sub(FilteredText, 5, string.len(FilteredText))

script.Parent.TextBack:FireClient(plr,name)

print("fired")

end)

To my knowledge, text isn’t filtered in Studio. Have you tried this in a live game?

I just tested it, its not working in real game.

You should be checking your console, surprised you’re not getting an error here. For some reason you’re also including a player parameter in OnClientEvent. This is not necessary. Your filtered text is actually being assigned to the plr parameter in OnClientEvent.

Like the server will implicitly receive the firing player first and then all arguments are shifted down, the client will only receive the arguments passed via FireClient and the server only needs the first argument, the player, to determine which client should be receiving the message.

Remove the plr parameter from OnClientEvent and try again.

I also notice that every time the button is clicked, you connect to OnClientEvent. You need to change this otherwise you’ll end up with a lot of connections and each time the button clicks, the amount of times the client processes a receive from FireClient goes up 1 each time. You should be using a RemoteFunction here anyway.

You can get a reference from the code of a model that I have made:
Model

Im confused, so this is what I have, so if its filtered, which is what I assume is when its censored, I do an error function, but if its clean, it should change a value, but its saying its always censored, any help?

local function errorfilter()
script.Parent.BadError.Visible = true
wait(2)
script.Parent.BadError.Visible = false
end

script.Parent.FilterText.OnServerEvent:Connect(function(player, text)
	local Char = player.Character
	if (text ~= "") and (string.len(text) <= 20) then
		
		local filteredText	= "";
		
		local Filtered, Failed = pcall(function()
			filteredText = game:GetService("Chat"):FilterStringForBroadcast(text, player);
		end);
		
		if (Filtered) then
			errorfilter()
		else
	     	player.stats.FirstName.Value = filteredText;	
		end;
		
	end;
end);

For a naming system like this, you don’t really need server-to-client communication, all you really need is to send a string to the server, of which the server (through OnServerEvent) which the server can then handle the filtering (using FilterStringAsync from TextService).

pcalls can be used here, but from my understanding from the latest code you’ve shown as well as the original, you’re doing something wrong.

A common misconception is that pcall will return a success result and an error. Yes, it does return a boolean showing if the script ran without any errors, but the errorMessage can also be the return result. i.e:

local Success, Result = pcall(function() return "A" end) -- This will return "A" through "Result" shown, Result can also be an error.

Using the pcall with a return result from FilterStringAsync, you could set the value to the filtered text if Success were to be true, or print the error (through Result) if it were to somehow fail Success == false.

Im sorry, im not really fluent when it comes to all that stuff. So this isn’t something I understand, Is there any other way to explain this to me.

I have been trying for about an hour now to see if I can get anywhere with your code, sort of, I made it so it funcitons, just not correctly, if the word is bad, it still does the good result.

local function errorfilter()
script.Parent.BadError.Visible = true
wait(2)
script.Parent.BadError.Visible = false
end

script.Parent.FilterText.OnServerEvent:Connect(function(player, text)
	local Char = player.Character
	if (text ~= "") and (string.len(text) <= 20) then
		
		local filteredText	= "";
		
		local Success, Result = pcall(function() -- This will return "A" through "Result" shown, Result can also be an error.
			filteredText = game:GetService("Chat"):FilterStringAsync(text, player, player)
			end);
		
		if (Result) then
	     errorfilter()
	else
		if (Success) then
	     	player.stats.Nickname.Value = filteredText;
		end;
		
		end;
		end;
end);

if (Result) then
That’s gonna call errorfilter no matter what, since Result will always return something. Also, else if can be a simple elseif or even an else.
You should use this instead:

if Success then
 player.stats.Nickname.Value = filteredText
else
 errorfilter()
end
1 Like

this still give me the same good outcome result even if its an obviously filtered word.

This code should work. Let me know if it works.

script.Parent.FilterText.OnServerEvent:Connect(function(player, text)
	Character	= player.Character;

	if (text ~= "") and (string.len(text) <= 20) then
		
		local filteredText	= "";
		
		local Filtered, Failed = pcall(function()
			filteredText	= game:GetService("Chat"):FilterStringForBroadcast(text, player);
		end);
		
		if (Filtered) then
			Character.Name	= filteredText;
		else
			-- Code
		end;
		
	end;
end);

No, still does the the thing where it always does the good outcome. sorry for the late response

The filter broadcast doesn’t work in studio by the way, If you want to test it, publish it on a private game and then try.