How to detect if a string is inappropriate?

Hey developers!

For my new game, I want players to be able to name their characters. Though if the string is inappropriate I don’t want hashtags, I just want it to say ‘Name not appropriate for Roblox.’. I think this would be the most efficient way, though I’m not quite sure.

function isallowed(string)
    if string.find(game:GetService("TextService"):FilterStringAsync(string), "#") then
        return false
    end
end

Any help would be appreciated!

Have a nice day!

3 Likes

Check if the filtered string is the same as the original string. If it isn’t, then it was blocked by the filter.


Edit: @KJry_s’s solution would work as well.

2 Likes

Instead I should suggest just filtering the string and returning it; A player can type “#” and you might confuse it with an inappropriate statement.

Use the following script:

local TextService = game:GetService("TextService")

local function filterString(filteredText)
	local success, errorMessage = pcall(function()
		return TextService:FilterStringAsync(text, fromPlayerId)
	end)
	
	if not success then
		warn("Error filtering text:", text, ":", errorMessage)
		-- Put code here to handle filter failure
	end
end

This was taken from here, but I edited it a bit.

Else I suggest getting the positions of the #'s when the player send the message and checking if any new #'s popped up.

2 Likes

I remember using this a little while ago, I did this.

FilterResult = text_service:FilterStringAsync(Message, PlayerUserId)
FilterToString = FilterResult:GetNonChatStringForUserAsync(PlayerUserId)

I am 99% sure that you can only filter text if you are in an actual game and not studio.
Hope this helps!

1 Like

I will be using the following code to achieve this.

local TextService = game:GetService("TextService")

local function filterString(filteredText)
	local success, errorMessage = pcall(function()
		return TextService:FilterStringAsync(text, fromPlayerId)
	end)
	
	if not success then
		warn("Error filtering text:", text, ":", errorMessage)
		-- Put code here to handle filter failure
	end
end

local function findAll(str, sub)
	local found = 0
	local positions = {}
	while(found)do
		found = found + 1
		found = str:find(sub, found)
		table.insert(positions, found)
	end
	return positions
end

local function filter(string)
   local filtered = filterString(string)

   if filtered then
      if findAll(string, "#") ~= findAll(filtered, "#") then
         return "Name not appropriate for Roblox."
      else
         return filtered
      end
   
end

Note when editing filtering does not work, as @deafaultyboii1324 said.
I am unsure if making a test server or team testing does or does not work though.

2 Likes

Thanks, everyone for your help!
I have come to the conclusion that this is the best option:

function isallowed(string, userid)
    if game:GetService("TextService"):FilterStringAsync(string, userid):GetNonChatStringForUserAsync(userid) == string then
        return true
    end
end
8 Likes

Thank you it help me so much! :D!!
Havent known about those for a while searching some and finally found this one xd.

1 Like