Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

What do the :FilterStringAsync() Parameters Mean?

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

1 answer

✓ Accepted answer

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 --// 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)

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

0

nice answer

2

Thanks :)

0

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?

2

I fixed this, I edited the script, just now.

2

The yield, the instance, has a function of where you send out the message.

0

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.

0

I wrapped the "FilerStringAsync()" and the "GetChatForUserAsync()" in individual pcall functions, and success prints as true for both of them.

2

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.

0

It is working perfectly, thank you so so much for all your help, you are amazing :D

1

Thank you! And glad to be of help!

This discussion is preserved as it was. New answers, comments, and votes are closed.