Text filtering not working

I’m trying to use text filtering on the text the player inputs, but I get the error “string expected got instance”. Is anyone able to help?

game.ReplicatedStorage.ChangeStageText.OnServerEvent:Connect(function(player, text)
	local function getFilteredMessage(textObject, toPlayerId)
		local TextService = game:GetService("TextService")
		local filteredTextResult = TextService:FilterStringAsync(textObject, toPlayerId)
		return filteredTextResult
	end
		
	script.Parent.SurfaceGui.TextLabel.Text = getFilteredMessage(text, player.UserId)
end)
1 Like

Show the script that is firing the server event. Maybe you added instance to the connect’s text argument.

script.Parent.MouseButton1Click:Connect(function()
	game.ReplicatedStorage.ChangeStageText:FireServer(script.Parent.Parent.TextBox.Text)
end)

The TextService:FilterStringAsync() sends back and instance as the return.

Use this instead it should work.

game.ReplicatedStorage.ChangeStageText.OnServerEvent:Connect(function(player, text)
	local function getFilteredMessage(textObject, toPlayerId)
		local TextService = game:GetService("TextService")
		local filteredTextResult = TextService:FilterStringAsync(textObject, toPlayerId)
		
		local filteredString = filteredTextResult:GetNonChatStringForUserAsync(toPlayerId)
		return filteredString 
	end
	
	script.Parent.SurfaceGui.TextLabel.Text = tostring(getFilteredMessage(text, player.UserId))
end)

tostring just in case :happy1:

Wrapping the returned value inside a tostring() call is unnecessary as the function only ever returns a single string type value.

@Towren
Is this filtering performed for chat messages between users or something like naming a pet? If the latter then keep using GetNonChatStringForUserAsync() as that’s what it is purposed for, if the former, then you should use GetChatForUserAsync() as that is supposed to be used to filter chat messages shared between users.