Could someone explain to me how to filter strings?
I am making my own chat for my game, so the messages sent by the players all have to be filtered before being displayed. I looked at the wiki article, but I didn't fully understand what all the parameters of the :FilterStringAsync()
function meant. This is what I have written so far (I will add pcall later):
ReplicatedStorage = game:GetService("ReplicatedStorage") FilterChat = ReplicatedStorage:WaitForChild("FilterChat") -- Remote Event that gets called when a player sends a message TextService = game:GetService("TextService") FilterChat.OnServerEvent:Connect(function(sender, Message) -- "Message" is the string for what the player wrote to be sent on the chat print(Message) local FilteredMessage = TextService:FilterStringAsync(Message) print(FilteredMessage) end)
This gives an error that basically says I didn't fill in all the parameters for :FilterStringAsync()
. I know there are two more from the wiki article, but I'm not sure what they mean or do, nor what I should put in them. All I want to do is simply filter the string so nothing inappropriate and nothing containing profanity can be sent over the chat, in the same way the roblox default chat filters the player's messages. Any help would be greatly appreciated, thank you for reading! :D
FilterStringAsync has 3 parameters.
1) StringToFilter (String)
local textService = game:GetService("TextService") local message = "Hello World!" textService:FilterStringAsync(message)
2) FromUserId (IntValue)
Here you will have to put the UserId of the player who has said that message, this will be the player who will be filtering the text.
We use UserId because that cannot be changed, as their name can be. So, when they send a message, we filter the message from that player instead of a different player. Players will send multiple messages, so we use their UserId to determine which message belongs to which player.
local textService = game:GetService("TextService") game.Players.PlayerAdded:Connect(function(player) player.Chatted:Connect(function(message) textService:FilterStringAsync(message,player.UserId) end) end)
3) TextContext (TextFilterContext)
local textContextPublic = Enum.TextFilterContext.PublicChat --// Public Chat local textContextPrivate = Enum.TextFilterContext.PrivateChat --// Private Chat
This is how it would look, completed.
local textService = game:GetService("TextService") game.Players.PlayerAdded:Connect(function(player) player.Chatted:Connect(function(message) textService:FilterStringAsync(message,player.UserId,Enum.TextFilterContext.PublicChat) end) end)
TextService yields an Instance, which is the Filtered Message from the player who is filtering the message. This is the message that you would want to send out.
The Yield is an Instance, and you can get the result from that using a function GetChatForUserAsync()
.
local actualMessage = filteredMessage:GetChatForUserAsync(player.UserId)
This is how your code should look like.
local ReplicatedStorage = game:GetService("ReplicatedStorage") local FilterChat = ReplicatedStorage:WaitForChild("FilterChat") local TextService = game:GetService("TextService") local contextType = Enum.TextFilterContext.PublicChat FilterChat.OnServerEvent:Connect(function(sender, Message) print(message) local filteredMessage = textServie:FilterStringAsync(message,player.UserId,Enum.TextFilterContext.PublicChat) local actualMessage = filteredMessage:GetChatForUserAsync(player.UserId) print(actualMessage) end)
This will print the Unfiltered message first, then filter the message second, and then finally, print the filtered message. You can use the variable actualMessage
to send a message out to all the other players chat, or use InvokeClient/InvokeServer to return the FilteredMessage.
But, I hope this helped explain how to use TextService! If this helped you, don't forget to select this as the answer!