Issue filtering a string

Hello. This should be simple, but no matter what I do, filtering a string just returns nil, and I don’t know how to fix it. Any help is appreciated. Thank you.
Server Script:

function filterString(player, str)
	if typeof(str) == "string" then
		local filteredInstance
		local filteredString
		local success, err = pcall(function()
			filteredInstance = game:GetService("TextService"):FilterStringAsync(str, player, Enum.TextFilterContext.PrivateChat)
			filteredString = filteredInstance:GetNonChatStringForUserAsync()
		end)
		if err then
			return "error"
		end

		return filteredString
	end
end
game.ReplicatedStorage.GameEvents.Filter.OnServerInvoke = filterString

Client Script:


local filter = game.ReplicatedStorage.GameEvents.Filter

script.Parent.SendButton.MouseButton1Click:Connect(function()
	if script.Parent.TextBox.Text then
		local text = script.Parent.TextBox.Text
		local filtered, changed = filter:InvokeServer(game.Players.LocalPlayer, text)
		print(filtered, text)
		if changed then
			game.ReplicatedStorage.GameEvents.Suggest:FireServer(tostring(filtered))
		else
			script.Parent.TextBox.Text = "Suggestion Filtered"
	    	wait(2)
	  	    script.Parent.TextBox.Text = filtered
		end
	end
end)


The print line always prints nil, and then the original text, so it’s not an issue with the source text

There’s no absolute need to pass through the LocalPlayer as a parameter, as Remote instances (RemoteEvents & RemoteFunctions) always give you the Player as the first parameter when you receive it within the server side

“The first parameter of the event handler is always the Player object of the client that calls it, and additional parameters follow.” - Remote Events & Functions

Also, I don’t believe you’re using the 2nd parameter of FilterStringAsync correctly as it requires a UserID of the Player you’re exactly trying to filter out here & you’re using the Player object as a whole:

I also recommend changing your variables a bit, so that you don’t accidentally pass through the incorrect data to send through & back

-- Client
local filter = game.ReplicatedStorage.GameEvents.Filter

script.Parent.SendButton.MouseButton1Click:Connect(function()
	if script.Parent.TextBox.Text then
		local text = script.Parent.TextBox.Text
		local filtered, changed = filter:InvokeServer(text) -- Just pass the text only
		print(filtered, text)
		if changed then
			game.ReplicatedStorage.GameEvents.Suggest:FireServer(tostring(filtered))
		else
			script.Parent.TextBox.Text = "Suggestion Filtered"
	    	wait(2)
	  	    script.Parent.TextBox.Text = filtered
		end
	end
end)
-- Server
function filterString(player, str)
	if typeof(str) == "string" then
		local filteredInstance
		local currentString
		local success, err = pcall(function()
			filteredInstance = game:GetService("TextService"):FilterStringAsync(str, player.UserId, Enum.TextFilterContext.PrivateChat)
			currentString = filteredInstance:GetNonChatStringForUserAsync()
		end)
		if err then
			return "error"
		end

		return currentString, filteredInstance
	end
end

game.ReplicatedStorage.GameEvents.Filter.OnServerInvoke = filterString
3 Likes

I tried these scripts and it always returned error. Do you know why this is happening?

After researching the API Reference, you supplemented no parameters for this method as well so we’d need to use the same Player.UserId idea that we had before

currentString should return back as a string datatype afterwards, so replace that variable with:

			currentString = filteredInstance:GetNonChatStringForUserAsync(player.UserId)

And if it doesn’t work after that, consider debugging it out with some print() statements :wink:

The filtering works, but now the local script doesn’t detect if it was filtered or not.
This is how I’m handling it:

script.Parent.SendButton.MouseButton1Click:Connect(function()
	if script.Parent.TextBox.Text then
		local text = script.Parent.TextBox.Text
		local filtered, changed = filter:InvokeServer(text) -- Just pass the text only
		local equal = tostring(filtered) == tostring(text)
		print(filtered, text, equal)
		if equal then
			game.ReplicatedStorage.GameEvents.Suggest:FireServer(tostring(filtered))
		else
			script.Parent.TextBox.Text = "Suggestion Filtered"
			wait(2)
			script.Parent.TextBox.Text = filtered
		end
	end
end)

Equal always prints true, but then the if statement basically doesn’t run. Any idea why this occurring?

It might have something to do with your FireServer method

If equal is printing back out as true as you stated, then we know for sure that our RemoteEvent should fire properly (Include a print within the equal conditional check to make sure), but something within our RemoteEvent doesn’t seem to be working or it could be that we’re passing in a different parameter

Extremely strange though on why it’d print true, if the conditional check won’t allow it (Just to make sure, try confirming that equal == true instead of just checking for equal)

I also noticed that I forgot to return back an additional parameter for the RemoteFunction, I don’t know if it’ll change anything but you can try it again and see if it works

Yeah, the remote event is firing. I just checked.
But I know the code in it works. I tested it before I added filtering.

This is the code that handles the server side

game.ReplicatedStorage.GameEvents.Suggest.OnServerEvent:Connect(function(player, text)
	local data = {
		["embeds"] = {{
			["author"] = {
				["name"] = player.Name .. "'s Suggestion" ,
				["icon_url"] = "https://www.roblox.com/Thumbs/Avatar.ashx?x=100&y=100&username="..player.Name
			},
			["description"] = "Suggestion: " .. text,
			["color"] = tonumber(0xFFFAFA),
			["fields"] = {
				{
					["name"] = "User ID:",
					["value"] = player.UserId,
					["inline"] = true
				}
			}
		}},
	}
	local finaldata = HTTP:JSONEncode(data)
	HTTP:PostAsync(webhookurl, finaldata)
end)

It worked before, so I don’t see why it doesn’t work now.

Unfortunately I don’t know much about how HTTPService works, but from what I see happening is that it looks like the one of your 2 methods at the end don’t seem to be working here

I don’t have much knowledge on why the case would be, as all I can suggest is print() and checking what you’re supposed to get as a result from the HTTP request so I’d consider creating another topic relevant to that issue

I’m marking this as a solution because I learned yesterday that the only way to send webhook requests to discord via Roblox is using a proxy, so that’s why it wasn’t working. Thanks for all of your help!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.