Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I use FilterStringAsync to make sure text is appropriate?

Asked by 4 years ago

I am trying to make a script to make sure text is appropriate but I don't get it.

I've just come up with this piece of code to test it:

local players = game.Players
local player = players.ChildAdded:Wait()

function filter(text)
    local TextService = game:GetService("TextService")
    local filteredTextResult = TextService:FilterStringAsync(text, player.UserId)
    return filteredTextResult
end

print(filter("hi"))

I don't get an error but in the output I get: "Instance" I'm not sure what to do with that.

1 answer

Log in to vote
0
Answered by
SCP774 191
4 years ago

You missed a very important step in your script.

FilterStringAsync doesn't return String, it returns an Instance (I like to call it "TextObject")

And then, you have to use :GetChatForUserAsync() or :GetNonChatStringForBroadcastAsync() to get the filtered String.

Example script:

local function getTextObject(message, fromPlayerId)
    local textObject
    local success, errorMessage = pcall(function()
        textObject = TextService:FilterStringAsync(message, fromPlayerId)
    end)
    if success then
        return textObject
    end
    return false
end

local function getFilteredBCMessage(textObject)
    local filteredMessage
    local success, errorMessage = pcall(function()
        filteredMessage = textObject:GetNonChatStringForBroadcastAsync()
    end)
    if success then
        return filteredMessage
    end
    return false
end

local function FilterString(senderName, msg)
    if msg then
        local sender = game.Players:FindFirstChild(senderName)
        local messageObject = getTextObject(msg, sender.UserId)
        if messageObject then
            local filteredMessage = getFilteredBCMessage(messageObject)
            return filteredMessage
        end
    else return nil
    end
end

print(FilterString("Hello! This String is filtered for boardcast! 123456 <-- This should be tagged!"))

P.S. Text filtering doesn't work in studio, it only works in-game.

Ad

Answer this question