Which String Filter to Use

Hey developers! I am making a custom chat system, and so string filtering in the context of chat is needed! I have 3 filter filtered options but Idk what to use (Not including deprecated methods as I want this to work long-term):

GetChatForUserAsync ,

GetNonChatStringForBroadcastAsync ,

GetNonChatStringForUserAsync

You see there is some issues with the Above methods.

:GetChatForUserAsync() Only Filters for one user. In otherwords, It Has a From UserID and a ToUserID, but considering I am making a public chat system with all members, I would make way too many calls, and have issues with rate limiting.

:GetNonChatStringForBroadcastAsync() Filtering, but outside the Context of a chat system, as the Name suggests “:GetNonChatStringForBroadcastAsync()”

:GetNonChatStringForUserAsync() isn’t used here as It’s mean for user-wide and not for everyone to see .

So which filter to use?

Any help is much appreciated !

1 Like

You should:

  • Send the correct data to each client individually.

or

  • Use Chat:FilterStringForBroadcast()
1 Like

This method is kind of deprecated. As mentioned On the Docs “Chat” Service is deprecated, And Roblox docs does mention to Use TextService Over these old Methods

“Text filtering should be done from a Script on the server using the similarly-named TextService:FilterStringAsync(), which uses a different set of parameters and return type.”

This doesn’t fix my issue per say as I will have rate limits, I think?. You can only call TextService:FilterAsync() a 1,000 times per minute as I have heard. While this doesn’t seem like an issue, if I were to say have a full server of 10 people, Thats 10 filters per message, and a max of 100 messages per minute. (Not to mention that each filter call Yields too!) While this isn’t too big of a deal, “ok” would be seen as message needed to be filtered 10 times for each client. (Also this resource will be public so some games may even have more people, and thus even cause bigger issues)

2 Likes

I see. In that case, I would assume that it is safe to use GetNonChatStringForBroadcastAsync (since it is mainly for broadcasting), unless there’s a misunderstanding documentation creator about the rate limits.

But I wouldn’t think that the chat service’s filtering mechanic, which is FilterStringAsync, will be affected by the deprecation of it. TextService should be used instead, but if you want to conserve requests, use the broadcast function in the TextFilterResult.

Edit: Just realized it appears TextService:FilterStringAsync is the recommended option, but not sure if it has the same rate limit.

1 Like

Yes sorry if I wasn’t clear, but yes it has that thousand limit, I have never used Chat:FilterAsync() calls so I don’t know about their ratelimit.

I’ll see what I can do with it…, or anyone has different ideas, Thanks for your help

1 Like

I meant TextService:FilterStringAsync

1 Like

I told you, this is for private messages and stuff.

There is a normal FilterStringForBroadcast method of chatservice, albeit older you probably shouldn’t use anything that says “non chat”

2 Likes

You should be using the new TextChatServide apis for custom chats.

1 Like

He cannot use testchatservice apis because they were designed to send messages and filter them automatically. However, he is creating a custom chat.

This is how I usually go about it.

local TextService = game:GetService("TextService")

-- Define our function with Luau type annotations
local function filterTextAsync(userId: number, textToFilter: string): string?
    assert(typeof(userId) == "number", "UserId must be a number")
    assert(typeof(textToFilter) == "string", "Text to filter must be a string")

    local success, filteredText = pcall(function()
        -- Assuming we're filtering for public chat, you can change the Enum if needed
        local textFilterResult = TextService:FilterStringAsync(textToFilter, userId, Enum.TextFilterContext.PublicChat)
        return textFilterResult:GetNonChatStringForBroadcast()
    end)

    if success then
        return filteredText
    else
        warn("Failed to filter text: " .. tostring(filteredText))
        return nil
    end
end

-- Example Usage:
local player = game.Players.LocalPlayer
local userId: number = player.UserId
local unfilteredText: string = "Your unfiltered text here"
local result: string? = filterTextAsync(userId, unfilteredText)
if result then
    print(result)
end