I need help or guidance with using TextFilter and GetNonChatStringForBroadcastAsync for displaying text on a TextLabel.
I have read through the TextFilter documentation thoroughly for the past 30 minutes and I’m still struggling to understand how I would use this. I’m trying to filter the SideOneLabel.Text and SideTwoLabel.Text and have tried multiple ways but I realize that I’m doing it wrong halfway through.
Function:
local function DisplayChoices()
local choice
if next(customChoicesQueue) then
local playerId, customChoice = next(customChoicesQueue)
choice = { side1 = customChoice.side1, side2 = customChoice.side2 }
customChoicesQueue[playerId] = nil
else
local randomNum = math.random(1, #choices)
choice = choices[randomNum]
end
SideOneLabel.Text = choice.side1
SideTwoLabel.Text = choice.side2
end
Any assistance or pointers on how I would filter the text with GetNonChatStringForBroadcastAsync would be greatly appreciated, - P.S; TextService has been declared as a variable earlier in the script. Thanks alot.
You need to be filtering these strings on the server, but I’m a bit unsure as to how these things are laid out in your case.
So filter the string on the server before adding it to your customChoicesQueue.
There are two separate calls that need to be done, both of which can fail so they need to be wrapped in a pcall to handle errors accordingly.
This can be done like this:
-- 1: obtain a TextFilterResult:
local ok, result = pcall(textService.FilterStringAsync, textService, string, userId, Enum.TextFilterContext.PublicChat)
if not ok then
warn('Failed to filter string:', result) -- failed to obtain a TextFilterResult so we return early because this request cannot proceed; do error handling logic here
return
end
-- 2: do the actual filtering on the TextFilterResult:
local ok, filteredString = pcall(result.GetNonChatStringForBroadcastAsync, result)
if not ok then
warn('Failed to obtain string to be broadcasted:', filteredString) -- again, failed, so do error handling logic here
return
end
-- now, filteredString will be a string that is properly filtered
print('The filtered string is:', filteredString)
The customChoicesQueue is handled by a RemoteEvent so I’ll add it in the listening script so it passes the filtered version and I’ll let you know how it goes, thanks alot.
Yup, it would be the string that needs to go through the filter. And then of course userId would be the user id of the person who’s making the request.