Help With Filtering System

I’m trying to make a thing that can hold potential spam bot / scam bot messages on hold until the person can solve a math equation.

I got everything working except for the filter when disabling it.

Here’s the code.

Module script inside of game.Chat.ChatModules.

local MyCodes = {"free", "bobux", "robux", "fr33", ".com", ".net", ".org"}
local ReplaceWith = "[Redacted]"


--- Could change this to hash out or do something like that. 
local function replaceCode(message, startIndex, endIndex)
	if game.ReplicatedStorage.FilterOn.Value == true then
	local startPart = string.sub(message, 1, startIndex - 1)
	local endPart = string.sub(message, endIndex)
		return startPart.. ReplaceWith .. endPart
		end
end

local function Run(ChatService)
	if game.ReplicatedStorage.FilterOn.Value == true then
	local function checkForCodes(sender, messageObject, channelName)
		local messageLower = string.lower(messageObject.Message)
		for i = 1, #MyCodes do
			while true do 
				local code = string.lower(MyCodes[i])
				local found = string.find(messageLower, code)
				if found then

					messageLower = replaceCode(messageLower, found, found + string.len(code))
						messageObject.Message = ""
						
				else
					break
				end
			end
		end
	end
if game.ReplicatedStorage.FilterOn.Value == true then
			ChatService:RegisterFilterMessageFunction("checkForCodes", checkForCodes)
			end
	end
	end

return Run

Server script.

game.Players.PlayerAdded:Connect(function(plr)

	plr.Chatted:Connect(function(msg)
		if string.match(msg, "free", 1) then
			game.ReplicatedStorage.SendCaptcha:FireClient(plr, tostring(msg))
		elseif string.match(msg, "robux", 1) then
			game.ReplicatedStorage.SendCaptcha:FireClient(plr, tostring(msg))
		elseif string.match(msg, "bobux", 1) then
			game.ReplicatedStorage.SendCaptcha:FireClient(plr, tostring(msg))
		elseif string.match(msg, ".com", 1) then
			game.ReplicatedStorage.SendCaptcha:FireClient(plr, tostring(msg))
		elseif string.match(msg, "fr33", 1) then
			game.ReplicatedStorage.SendCaptcha:FireClient(plr, tostring(msg))
		elseif string.match(msg, ".net", 1) then
			game.ReplicatedStorage.SendCaptcha:FireClient(plr, tostring(msg))
		elseif string.match(msg, ".org", 1) then
			game.ReplicatedStorage.SendCaptcha:FireClient(plr, tostring(msg))
		
		
		
		end
	end)
end)

Local script that sends the message.

local text = script.Parent.Parent.TextBox
local answer = 52
local button = script.Parent
local message = script.Parent.Parent.Message
local filteredMessage = game.Chat:FilterStringForBroadcast(tostring(message.Value), game.Players.LocalPlayer)
local answer
local equation = script.Parent.Parent.EquationValue
function update()
	if equation.Value == "5+5" then
		answer = 5+5
	elseif equation.Value == "50+2" then
		answer = 50+2
	elseif equation.Value == "50+50" then
		answer = 50+50
	elseif equation.Value == "10-1" then
		answer = 10-1
	elseif equation.Value == "5+2+8+9-2" then
		answer = 5+2+8+9-2
	elseif equation.Value == "25+25" then
		answer = 25+25
	
	
	
	
	end
end
button.MouseButton1Click:Connect(function()
	update()
	if tonumber(text.Text) == answer then
		game.ReplicatedStorage.FilterOn.Value = false
		wait(0.1)
		script.Parent.Parent.Submit.Visible = false
		script.Parent.Parent.Cancel.Visible = false
		script.Parent.Parent.TextBox.Visible = false
		script.Parent.Parent.Equation.Visible = false
		script.Parent.Parent.Text.Text = "Your answer is correct, your message has now been sent."
		game.ReplicatedStorage.DefaultChatSystemChatEvents.SayMessageRequest:FireServer(message.Value,"all")
		
		wait(5)
		game.ReplicatedStorage.FilterOn.Value = true
		script.Parent.Parent.Submit.Visible = true
		script.Parent.Parent.Cancel.Visible = true
		script.Parent.Parent.TextBox.Visible = true
		script.Parent.Parent.Equation.Visible = true
		script.Parent.Parent.Visible = false
		script.Parent.Parent.Text.Text = "Your message was flagged by our custom chat filter. In order to send your message, you must solve this math equation to prove you are a human."
		game.Lighting.Blur.Enabled = false
	else
		script.Parent.Parent.Text.Text = "Incorrect answer, try again."
		wait(4)
		script.Parent.Parent.Text.Text = "Your message was flagged by our custom chat filter. In order to send your message, you must solve this math equation to prove you are a human."
	end
end)

Those are the only scripts that are related to the problem.

Basically, the issue is either if filteron is off it will still delete the messages. I want it to allow the message to be sent when it is send by the localscript.

2 Likes