Hi,
I’m implementing functionality into my game to filter text sent by the client, and for ease of use I’ve created the following function:
function GeneralUtils.FilterText(plr: Player, textToFilter: string, filterMethodToUse: "1" | "2" | "3"): { Text: string, Censored: boolean }
local textFilterResult: TextFilterResult | nil = nil
local success, errorMessage = pcall(function()
textFilterResult = TextService:FilterStringAsync(textToFilter, plr.UserId)
end)
if not success then
warn(`Error filtering text: {textToFilter}, :, {errorMessage}`)
return { Text = "", Censored = false }
end
local stringToReturn = textToFilter
local success2, errorMessage2 = pcall(function()
if filterMethodToUse == "1" then
print(textFilterResult:GetChatForUserAsync(plr.UserId))
-- print(stringToReturn)
elseif filterMethodToUse == "2" then
print(textFilterResult:GetNonChatStringForBroadcastAsync())
-- print(stringToReturn)
elseif filterMethodToUse == "3" then
print(textFilterResult:GetNonChatStringForUserAsync(plr.UserId))
-- print(stringToReturn)
else
warn("Filter method not selected")
end
end)
if not success2 then return { Text = "", Censored = false } end
return {
Text = stringToReturn,
Censored = textToFilter ~= stringToReturn -- if they aren't equal, then the text was censored
}
end
– called from another script
print(GeneralUtils.FilterText(plr, name, "1"))
print(GeneralUtils.FilterText(plr, name, "2"))
print(GeneralUtils.FilterText(plr, name, "3"))
As seen above, I’ve tested it with all available TextFilterResult methods, however strings that should be censored aren’t. After rereading the documentation examples I haven’t found out where I’ve gone wrong.
Any help would be great thanks!