Need help with a filtering function

Hello! I have recently seen this in the Text and Chat Filtering article on the Developer Hub:


So I attempted to wrap my existing filtering function in a pcall(), since I can do an error message on the client saying that there was an error with the chat filter.

The code:

success, filterFunction.OnServerInvoke = pcall(function(plr, unfilteredText)
	filteredText = txtSer:FilterStringAsync(unfilteredText, plr.UserId)
	return filteredText:GetNonChatStringForBroadcastAsync()
end)

The OG code:

filterFunction.OnServerInvoke = function(plr, unfilteredText)
	filteredText = txtSer:FilterStringAsync(unfilteredText, plr.UserId)
	return filteredText:GetNonChatStringForBroadcastAsync()
end

The error

image
If you can help, please let me know. Thanks! WE

You put success, filterFunction so it expects you to put a string there.

1 Like

Wait so success is the string or filterFunction?

The error is happening because pcall returns 2 values. You could ignore the second variable, but since you put a comma after success, so it expects the name of the next variable. However, it gets the event connection, so it errors.

I would do this then? I tried this before and it didn’t work.

filterFunction.OnServerInvoke = pcall(function(plr, unfilteredText)
	filteredText = txtSer:FilterStringAsync(unfilteredText, plr.UserId)
	return filteredText:GetNonChatStringForBroadcastAsync()
end)
success, failure = filterFunction.OnServerInvoke = pcall(function(plr, unfilteredText)
	filteredText = txtSer:FilterStringAsync(unfilteredText, plr.UserId)
	return filteredText:GetNonChatStringForBroadcastAsync()
end)

This was underlined:
image

success, failure = filterFunction.OnServerInvoke =

Sorry Im pretty new to pcalls() since none of my code uses them. There is no DevHub article even though there should be one.

I think you should put the pcall into its own function:

function()
    local success, failure = pcall(function()
        --function stuff
    end)
    --handle success and stuff
end
1 Like

This, but remove the function() end stuff at the bottom and top.