How do I fix this Filter Function

Hello developers, I am trying to make a warning system and didn’t know how to moderate it. I made a post asking about it and someone replied to me with some code then never got back when I told him that it was erroring. The problem is, this code filters everything not just bad words. I have tried what I can and can’t figure out how to make it work. Can anyone help me?

function f(message)
	local TextService = game:GetService("TextService")
	local RunService = game:GetService("RunService")
	if RunService:IsStudio() then return message end
	local Success, Error = pcall(function()
		message = TextService:FilterStringAsync(message, game.CreatorId)
	end)
	if Success then
		local FilterSuccess, FilterError = pcall(function()
			message = message:GetNonChatStringForBroadcastAsync()
		end)
		if FilterSuccess then
			return message
		end
	end
	local result = ""
	for i = 1, string.len(message) do
		result = result .. "#"
	end
	return result
end

Pay attention to this part at the end:

This code will just replace the entire message with #s (even the spaces).
There are a few returns above that one. The fact that you’re always getting this one means that all of the code above that is failing.

This function should, in theory:

  • Return the message as-is in Studio
  • Get a TextFilterResult for the message, then a filtered message for broadcast from that result, and return that message if none of these steps fail
  • Return the entire message’s length in #s if none of the above has been returned yet

The annoying part is that all of this code seems correct.
Please, please put print("pcall 1", Success, Error) and print("pcall 2", FilterSuccess, FilterError) immediately after the respective pcalls. It’s possible that there’s a typo or the like somewhere, and you’re not hearing about it because the pcalls muffle the errors.