Help with adding text filtering to custom chat

I have made a custom chat gui that sends messages but the one problem is that it’s not filtered, meaning you can swear without it getting censored. I have been looking at this post on the wiki (Text and Chat Filtering)
and I don’t know how to implement it into the scripts I have. If I could get some help on how to into the script that would be great!

Server Script

local event = game:GetService('ReplicatedStorage'):WaitForChild('Chatted')

     


    event.OnServerEvent:connect(function(player, nameColor, message, messageColor)
    	event:FireAllClients(player, nameColor, message, messageColor)
end)

Local Script

local colors = {'Deep orange', 'Toothpaste', 'Lime green', 'Really red', 'Cyan', 'Gold'}


local TextService = game:GetService("TextService")
local player = game.Players.LocalPlayer
local nameColor = BrickColor.new(colors[math.random(1, #colors)]).Color
local messageColor = BrickColor.new('Institutional white').Color
local messageBox = script.Parent:WaitForChild('InputFrame'):FindFirstChild('MessageBox')
local UIS = game:GetService('UserInputService')
local event = game:GetService('ReplicatedStorage'):WaitForChild('Chatted')



function onInput(Input, GPE)
	if GPE then return end
	if Input.KeyCode == Enum.KeyCode.Slash then
		messageBox:CaptureFocus()
	end
end

function onFocus()
	if messageBox.Text == 'Press \'/\' to type a message.' then
		messageBox.Text = ''
	end
end



function onFocusLost(EnterPressed)
	if EnterPressed then
		if messageBox.Text ~= '' then
			event:FireServer(nameColor, messageBox.Text, messageColor)
			messageBox.Text = 'Press \'/\' to type a message.'
		else
			messageBox.Text = 'Press \'/\' to type a message.'
		end
	else
		if messageBox.Text == '' then
			messageBox.Text = 'Press \'/\' to type a message.'
		end
	end
end


function addLine(message, Label)
	local chatBox = script.Parent:FindFirstChild('Frame')
	local newMessage = Instance.new('Frame', chatBox)

	newMessage.Size = UDim2.new(1, -10, 0, 20)
	newMessage.Position = UDim2.new(0, 5, 1, -25)
	newMessage.BackgroundTransparency = 1
	newMessage.ClipsDescendants = true
	local lineMessage = ''
	while Label.TextFits == false do
		lineMessage = string.sub(message, -1) .. lineMessage
		message = string.sub(message, 1, string.len(message) - 1)
		Label.Text = message
	end
	
	for _, oldMessage in pairs (chatBox:GetChildren()) do
		if oldMessage ~= newMessage then
			oldMessage.Position = oldMessage.Position + UDim2.new(0, 0, 0, -newMessage.AbsoluteSize.Y + 5)
		end
	end
	
	local messageLabel = Instance.new('TextLabel', newMessage)
	messageLabel.BackgroundTransparency = 1
	messageLabel.Font = 'SourceSansBold'
	messageLabel.TextColor3 = messageColor
	messageLabel.FontSize = 'Size18'
	messageLabel.TextXAlignment = 'Left'
	messageLabel.Text = lineMessage
	messageLabel.Size = UDim2.new(1, -10, 0, 20)
	messageLabel.TextStrokeTransparency = 0.6
	messageLabel.Position = UDim2.new(0, 5, 0, 0)
	
	local LineTag = Instance.new('StringValue', messageLabel.Parent)
	LineTag.Name = 'LineTag'
	local LineTag2 =Instance.new('StringValue', Label.Parent)
	LineTag2.Name = 'LineTag'
	
	if messageLabel.TextFits ~= true then
		addLine(lineMessage, messageLabel)
		print('addingling')
	else
		for _, oldMessage in pairs (chatBox:GetChildren()) do
			if not oldMessage:FindFirstChild('LineTag') then
				oldMessage.Position = oldMessage.Position + UDim2.new(0, 0, 0, -newMessage.AbsoluteSize.Y + 5)
			end
		end
	end
	LineTag:Destroy()
	LineTag2:Destroy()
end

function onEvent(sender, nameColor, message, messageColor)
	
	
	
	
	
	local chatBox = script.Parent:FindFirstChild('Frame')
	local newMessage = Instance.new('Frame', chatBox)
	newMessage.Size = UDim2.new(1, -10, 0, 20)
	newMessage.Position = UDim2.new(0, 5, 1, -25)
	newMessage.BackgroundTransparency = 1
	newMessage.ClipsDescendants = true
						
	local nameLabel = Instance.new('TextLabel', newMessage)
	nameLabel.BackgroundTransparency = 1
	nameLabel.Font = 'SourceSansBold'
	nameLabel.TextColor3 = nameColor
	nameLabel.FontSize = 'Size18'
	nameLabel.TextXAlignment = 'Left'
	nameLabel.Text = '[' .. sender.Name .. ']:'
	nameLabel.Size = UDim2.new(0, nameLabel.TextBounds.X + 5, 0, 20)
	nameLabel.TextStrokeTransparency = 0.6
						
	local messageLabel = Instance.new('TextLabel', newMessage)
	messageLabel.BackgroundTransparency = 1
	messageLabel.Font = 'SourceSansBold'
	messageLabel.TextColor3 = messageColor
	messageLabel.FontSize = 'Size18'
	messageLabel.TextXAlignment = 'Left'
	messageLabel.Text = message
	messageLabel.Size = UDim2.new(1, -nameLabel.AbsoluteSize.X, 0, 20)
	messageLabel.Position = nameLabel.Position + UDim2.new(0, nameLabel.AbsoluteSize.X, 0, 0)
	messageLabel.TextStrokeTransparency = 0.6
	
	if messageLabel.TextFits ~= true then		
		addLine(message, messageLabel)
	else
		for _, oldMessage in pairs (chatBox:GetChildren()) do
			if oldMessage ~= newMessage then
				oldMessage.Position = oldMessage.Position + UDim2.new(0, 0, 0, -newMessage.AbsoluteSize.Y + 5)
			end
		end
	end
end


UIS.InputBegan:connect(onInput)
event.OnClientEvent:connect(onEvent)
messageBox.Focused:connect(onFocus)
messageBox.FocusLost:connect(onFocusLost)

You could use FilterStringForBroadcast.

Would that work? Because I have the default chat disabled and the chat I have is a gui I made.

It would work however as far as I know FilterStringForBroadcast should be fine server side rather than locally. You would have to filter it before firing the message to all clients. Someone correct me if I’m wrong about the server side vs local side.

I’ve tried it in both scripts but it doesn’t seem to be doing anything.

Another option is using the TextService’s FilterStringAsync. The Roblox Developer page for Chat:FilterStringForBroadcast suggests using FilterStringAsync on the server instead since FilterStringForBroadcast may be disabled in the future.

Local textService = game:GetService("TextService")
Local filteredText

filteredText = textService:FilterStringAsync(textString,playerID,textContext)

I suggest reading this article on the textContext since it seems to tie into custom chat GUIs.

Avoid using broadcast filtering whenever possible. It is the harshest filter. Instead, filter for each individual player and then send them the new message.