FilterStringAsync has 3 parameters.
1) StringToFilter (String)
- Here you will have to put the message that you would want to have filtered by the ROBLOX service, TextService.
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)
- This is optional because the default TextContext is private chat, but if you wanted to change it you would do something like this.
local textContextPublic = Enum.TextFilterContext.PublicChat
local textContextPrivate = Enum.TextFilterContext.PrivateChat
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)
Resource
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!
10 comments
nice answer
Thanks :)
Thank you so much for the detailed answer! The first print functions does print the unfiltered message, but the second print function prints "Instance". Do you know how I could fix this?
I fixed this, I edited the script, just now.
The yield, the instance, has a function of where you send out the message.
It comes out as an actual string now, but the filtered string is printing the same thing as the unfiltered string, no matter what profanity I input to test it. I allowed studio access to API services, but the string still isn't filtering.
I wrapped the "FilerStringAsync()" and the "GetChatForUserAsync()" in individual pcall functions, and success prints as true for both of them.
It doesn't work in studio because roblox doesn't filter studio messages, it will only work in live servers. I know this because I tested it in both studio and live servers.
It is working perfectly, thank you so so much for all your help, you are amazing :D
Thank you! And glad to be of help!