Chat filters can be used to format messages, run special commands or prevent certain messages from displaying in the chat log which is what will be demonstrated in this tutorial.
IMPORTANT: You should never modify the default chat modules that are loaded by the Chat Service when the game starts. The default module scripts add and control the chat functionality and if modified or prevented to run by a custom module script, ROBLOX may take action against the game and/or your account. This tutorial will show you how to add a custom chat filter, but you must ensure there are no script errors in any custom module script added to chat modules that may prevent others from running.
Adding Custom Chat Module Scripts To add a custom chat module script, you will first need to create the ChatModules Folder in Chat and add a BoolValue to this folder to tell the Chat Service to load the default module scripts.
Find "Chat" in the Explorer Window and add a Folder (rename to "ChatModules") Add a BoolValue to the ChatModules folder (rename to "InsertDefaultModules" and check the box for Value)
Your Explorer should now look like the following:
At this point you should hit "Play" and verify that the ChatModules folder loads with the default module scripts.
If you do not see the module scripts, go back and check the BoolValue for the correct name and that the Value property is checked.
Adding Restricted Words Filter Now that we have the ChatModules folder and the default scripts loading, lets add a Module Script to the ChatModules folder and name it "RestrictedWordsFilter". In the RestrictedWordsFilter module script, we will set up the functions needed for adding the filter to our Chat Service. This requires a "Run" function which is called by the Chat Service when the game starts and we will register our custom filter using "ChatService:RegisterFilterMessageFunction" to bind our custom function to the Chat Service Filters so it will run the function when a chat message is sent by a player. I have added the custom function "processMessage".
local Chat = game:GetService("Chat")
local ReplicatedModules = Chat:WaitForChild("ClientChatModules")
local function Run(ChatService)
local function processMessage(sender, messageObject, channelName)
end
ChatService:RegisterFilterMessageFunction("RestrictedWordsFilter", processMessage)
end
return Run
Let's modify our custom function to prevent "FREE ROBUX" from ever displaying in the chat log for our game using the lua function 'string.find' . If the word appears in a message, we will set the MessageType to "Filtered" to prevent it from displaying in the chat log. (NOTE: This will generate an output message that there is no creator for the message. You can add a custom MessageCreatorModule to prevent this, but this will not be provided in this tutorial. )
local function processMessage(sender, messageObject, channelName)
if(string.find(messageObject.Message, "FREE ROBUX")) then
messageObject.MessageType = "Filtered"
end
end
Run the game and type FREE ROBUX in a chat message. You can also type "Hey want to earn FREE ROBUX" or "FREE ROBUX for everyone!" and verify the same result that the message does not display in chat. You may also want to try 2-Player local server to verify normal chat messages and whispers are working. If all seems okay, then lets continue!
You might notice that the user can still type Free Robux or FrEe RoBuX to get around your filter. We can format the message with lowercase letters and remove spaces before comparing it.
local function processMessage(sender, messageObject, channelName)
local messageFormatted = string.gsub(string.lower(messageObject.Message)," ","")
if(string.find(messageFormatted, "freerobux")) then
messageObject.MessageType = "Filtered"
end
end
When you test now, any variation of free robux should be filtered.
Awesome, but there may be more words you want to filter. The easiest way to add additional words without creating a separate check is to create a table of words and modify the function to iterate through the table and check each word. (NOTE: If you are using a "." in your string, you will need to escape it with a "%" as shown in the example below)
local function processMessage(sender, messageObject, channelName)
local messageFormatted = string.gsub(string.lower(messageObject.Message)," ","")
local restrictedWords = { "freerobux", "rblx%.gg", "rb|x%.gg", "rbix%.gg",
"bux%.life", "bux%.iife", "bux%.llfe", "bux%.ilfe", "tonsofrobux","bloxland" }
for _,word in pairs(restrictedWords) do
if string.find(messageFormatted, word) then
messageObject.MessageType = "Filtered"
return true
end
end
end
And there you have it! Your very own restricted words filter! Here is the code in its entirety with optional added lines for kicking the player if they use one of your restricted words.
local Chat = game:GetService("Chat")
local ReplicatedModules = Chat:WaitForChild("ClientChatModules")
local function Run(ChatService)
local function processMessage(sender, messageObject, channelName)
local messageFormatted = string.gsub(string.lower(messageObject.Message)," ","")
local restrictedWords = { "freerobux", "rblx%.gg", "rb|x%.gg", "rbix%.gg",
"bux%.life", "bux%.iife", "bux%.llfe", "bux%.ilfe", "tonsofrobux","bloxland" }
for _,word in pairs(restrictedWords) do
if string.find(messageFormatted, word) then
messageObject.MessageType = "Filtered"
local speaker = ChatService:GetSpeaker(sender)
speaker:GetPlayer():Kick()
return true
end
end
end
ChatService:RegisterFilterMessageFunction("RestrictedWordsFilter", processMessage)
end
return Run
This is my first tutorial! I hope you enjoyed it and will contribute your own tutorials to Lua Learning!
-Rumpshaka