Why this text filtering doesn't work?

Hello, I tried to make a string filtering system for my game. My goal is making a function that returns a bool value. If this string filtering to playes this bool value should be true else bool value should be false

The issue is the bool value is always true. I think I made a mistake in one of scripts. Btw I don’t know how to use filtering systems and I didn’t find a good tutorial on youtube.

Here is scripts:

First script is a local script inside StarterGui/ComputerGui/Background

local UserInputService = game:GetService("UserInputService")
local TextService = game:GetService("TextService")
local player = game:GetService("Players").LocalPlayer

local function textFiltered(text)
	return game.ReplicatedStorage.FilterStringFunction:InvokeServer(text)
end

local function onKeyPress(input)
	if input.KeyCode == Enum.KeyCode.Return and script.Parent.BoxFrame.TextBox.Text ~= ""  then
		script.Parent.TuturialFrame.Visible = false
		local texttofilter = script.Parent.BoxFrame.TextBox.Text
		
		if textFiltered(texttofilter) then
			print("text filtered")
		else
			game.ReplicatedStorage.SendMessageEvent:FireServer(script.Parent.BoxFrame.TextBox.Text)
			script.Parent.BoxFrame.TextBox.Text = ""
			wait(0.35)
			script.Parent.BoxFrame.TextBox:CaptureFocus()
		end
	end
end

UserInputService.InputBegan:Connect(onKeyPress)

Also there is remote function inside replicated storage

This is other script in ServerScriptService

local TextService = game:GetService("TextService")
local FilterStringFunction = game:GetService("ReplicatedStorage").FilterStringFunction
FilterStringFunction.OnServerInvoke = function(player, text)
	return TextService:FilterStringAsync(text, player.UserId, Enum.TextFilterContext.PublicChat) ~= text
end

Why this function always returns true. Btw I don’t want filtered. I need a function that returns is this string filtered string or not but I don’t know how to make this also I tried here.

Also this is my first topic.

Filtering doesn’t work in studio, make sure to test ingame. If it still doesn’t work, tell me.

1 Like

Yes, it doesn’t work in game. I looked the output in developer console. The output is always “text filtered”. One of scripts print that output

Why isn’t roblox filtering the chat itself? Doesn’t roblox automatically filter the chat whether you have a script or not?

This filtering system for another thing not chat. I tried to make a game that you can speak with AI. You can type your message in textbox so I need a filtering system to prevent bad words.

1 Like

Change your server script to this:

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

local function getTextObject(message, fromPlayerId)
	local textObject = nil
	
	local success, errorMessage = pcall(function()
		textObject = TextService:FilterStringAsync(message, fromPlayerId)
	end)
	
	if success then
		return textObject
	elseif errorMessage then
		print("Error generating TextFilterResult:", errorMessage)
	end

	return false
end

local function getFilteredMessage(textObject)
	local filteredMessage = nil
	
	local success, errorMessage = pcall(function()
		filteredMessage = textObject:GetNonChatStringForBroadcastAsync()
	end)
	
	if success then
		return filteredMessage
	elseif errorMessage then
		print("Error filtering message:", errorMessage)
	end
	
	return false
end

local function isTextValid(player, text)
	local messageObject = getTextObject(text, player.UserId)
	local filteredText = ""
	filteredText = getFilteredMessage(messageObject)
	
	return filteredText ~= text
end

FilterStringFunction.OnServerInvoke = function(player, text)
	return isTextValid(player, text)
end

Your text filtering code was wrong.

1 Like

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