Issues with TextService

Hi! I recently tried to add filtering in my rp name changer i follow this tutorial : https://www.youtube.com/watch?v=trKR0S1zaXs but it’s dosen’t work and i don’t get any error in the output. Here is my code in a server script :

local TextService = game:GetService("TextService")
local FilteredInstance = TextService:FilterStringAsync(Name, player.UserId,Enum.TextFilterContext.PublicChat)
local FilteredString = FilteredInstance:GetNonChatStringForUserAsync(player.UserId)
if FilteredString == Name then
	playerClass:ChangeRpName(Name)
	ChangeRpNameRemote:FireClient(player, true)
else
	ChangeRpNameRemote:FireClient(player, false)
end

Thanks in advance!

1 Like

There’s a documentation on the official docs site. It should help you with your problem.

I already read it and i copy what they do and still don’t work

This shouldn’t be too hard to implement:

local function getTextObject(message, fromPlayerId)
	local textObject
	
	local success, errorMessage = pcall(function()
		textObject = TextService:FilterStringAsync(message, fromPlayerId)
	end)
	
	if success then
		return textObject
	elseif errorMessage then
		print("Error generating TextFilterResult:", errorMessage)
	end
	
	return false
end

local function getFilteredBroadcast(textObject)
	local filteredMessage
	
	local success, errorMessage = pcall(function()
		filteredMessage = textObject:GetNonChatStringForBroadcastAsync()
	end)
	
	if success then
		return filteredMessage
	elseif errorMessage then
		print("Error filtering message:", errorMessage)
	end
	
	return "nil"
end

local function getFiltered(player, text)
	if text ~= "" then
		-- Filter the incoming message and send the filtered message
		local messageObject = getTextObject(text, player.UserId)
		local filteredText = ""
		
		filteredText = getFilteredBroadcast(messageObject)
		
		return filteredText
	end
end

local filteredString = getFiltered(PLAYER_OBJECT, STRING)
print(filteredString)

I don’t know how your entire scripts work together, so this is as much as I can do with the information I have.

Are you testing this in studio? It won’t work in studio, as If I remember correctly, it is a safety concern. If you wanna test the chat filter it has to be in the roblox appilication (also meaning making sure you publish your changes so they are effected inside the roblox appilication)