Need help with filtering text

I am trying to filter a text that a player enters into a TextBox, but I do not have much experience with filtering text and when I used print() on the filtered result, it appeared as “nil”.

Note that I did not get an error when the script ran.

Local Script:

function FilterText(text)
	local player = game:GetService("Players").LocalPlayer
	game.ReplicatedStorage.FilterText:FireServer(player,text)
	game.ReplicatedStorage.FilterText.OnClientEvent:Wait()
	game.ReplicatedStorage.FilterText.OnClientEvent:Connect(function(Filteredtext)
		print(Filteredtext)
		if Filteredtext == false then
			return false
		elseif
			Filteredtext ~= text then
			return false
		else
			return Filteredtext
		end
	end)
end

ServerScriptService:

game.ReplicatedStorage.FilterText.OnServerEvent:Connect(function(_,player,text)
	print(player)
	print(text)
	local filteredText = ""
	local success, errorMessage = pcall(function()
		filteredTextResult = game:GetService("TextService"):FilterStringAsync(text, player.UserId)
	end)
	print(success)
	print(errorMessage)
	if success == false or errorMessage == true then
		game.ReplicatedStorage.FilterText:FireClient(player,false)
	else
		game.ReplicatedStorage.FilterText:FireClient(player,filteredTextResult)
		print(filteredTextResult)
	end
end)

It only works in the actual game test, not in studio.

1 Like

It still shows up as nil when I test it on Roblox

You don’t need the underscore.

I have the underscore becasue for some reason the second parameter is player and the third one is text so I had to silence the first parameter

That is because you don’t need player here. Get rid of player on this line and the underscore where he mentioned. FireServer automatically includes player.
That won’t fix your overall problem though.

Hey! I’m not sure if this will help but it’s worth a try.

Localscript;
game.ReplicatedStorage.FilterText:FireServer(text)

Serverscript;
game.ReplicatedStorage.FilterText.OnServerEvent:Connect(function(player, text)
local TextService = game:GetService(“TextService”)
local filteredTextResult = TextService:FilterStringAsync(text, player.UserId


If you’re trying to return the filtered text, I’d recommend using a remote function which would look something like this;

Server Script;
local thing = RemoteFunction
local TextService = game:GetService(“TextService”)
thing.OnServerInvoke = (function(player, textToFilter)
local filteredTextResult = TextService:FilterStringAsync(textToFilter, player.UserId)
return filteredTextResult

end)
Localscript;
local filter = RemoteFunction
local filteredText = filter:InvokeServer(Text)
Text = filteredText

Customize to your needs!
You can also refer to this for more information! Text Filtering | Documentation - Roblox Creator Hub

2 Likes