I misunderstood FilterStringAsync*

Reluctantly trying to put filters in my scripts…

Player.Chatted:Connect(function(chatmessage)
	FiltersAreActualDooDooService = game:GetService("TextService")
	local WhatHeSaid = chatmessage
	repeat
		wait()
		local succ, err = pcall(function()
			WhatHeSaid = FiltersAreActualDooDooService:FilterStringAsync(WhatHeSaid, Player.UserId)
		end)
		if err then
			print(err)
		else
			print(WhatHeSaid)
		end
	until succ
--...

It’s going good, and then I see what “WhatHeSaid” printed as.

image
Don’t clean your glasses or wipe your screen. It returned an INSTANCE.
image

What am I supposed to do with this? I wanted a string. What is this?

1 Like

So, let me just direct you to the dev wiki for this one:


Apparently, TextService:FilterStringAsync returns a TextFilterResult instance, which you can use one of 3 methods to return the string you’re looking for:

When something is unintuitively giving you something you don’t recognize, I recommend you do tons of research and stay inquisitive so that it eventually makes sense on your own.
For example, if a function is returning an instance and you don’t know why, you could probably print its .ClassName property, which every instance has, so that you can look that up on the dev wiki.

5 Likes

A note in the future that you should try and make use of protected calls properly - or rather, in such a way that’s readable and maintainable. The loop is unnecessary because you’re not going to get a success return by repeatedly calling the method. Furthermore, you should wrap the method instead of an anonymous function.

local TextService = game:GetService("TextService") -- Don't put this in the function

Player.Chatted:Connect(function (ChatMessage)
    local success, result = pcall(TextService.FilterStringAsync, TextService, ChatMessage)

    if not success then
        warn(result)
    else
        print(result)
    end

    -- ..
end)

The only method you need to wrap in a protected call is the FilterStringAsync method as internally it is a web call. Once you get the TextFilterResult object back from FilterStringAsync, any of the methods you call are guaranteed to succeed as they simply return strings that were passed to TextFilterResult.

2 Likes

It was in the function because I was trying to make it portable…
thank you for the knowledge

Not related to the issue but you shouldn’t implement retry logic for the filtering methods according to the developer hub.

1 Like