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):
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 .
“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)
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.
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