Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How would I make this censor script allow capital letters?

Ok so I'm trying to make a censorship filter for my chat, however the method I'm using makes it so that capital letters return as lower case. Obviously they do this because of the string.lower, but how can I make it so that it censors and returns to the normal case?

		for N,T in ipairs (CensoredList) do

			Button.Text = string.gsub((string.lower(Button.Text)), (string.lower(T)), "###")

		end	

Current revision attempt -

local original = Button.Text
local lower = string.lower(original)

for _, word in next, CensoredList do
    local i, j = string.find(lower, word)
    while i and j do
        original = string.sub(original, 1, i - 1) .. string.rep("#", j - i + 1) .. string.sub(original, j + 1)
        i, j = string.find(lower, word, j)
    end
end

Button.Text = original

2 answers

5 comments

0

Why not? It could be a good learning experience. However, this function is great if you want the exact filtering that Roblox uses.

0

i need to make my own, and the filterforasync only works with traditional roblox chat

1

No it doesn't, the function returns the string filtered.

0

For the context of a ROBLOX game, you must filter user input through FilterStringForPlayerAsync.

0

It's still a good learning experience, but the one you write on your own isn't what you should be using to filter text in your ROBLOX game.

In order to accomplish this task, we will have to sadly ignore the simplicity of string.gsub.

The way I approached this solution was by storing both the original case string and the lower case string. I then use a loop to search for the start and end index (i and j respectively) for each of the matches using string.find. I break out of the loop when there is no match found (i == nil), and then start all over again for the next word in the profanity table.

To replace the match, I take the substring up to match, concatenate it with a repetition of '#' symbols, and concatenate that with everything after the match.

###Code:

local profanity = {"cat", "dog"}

local original = "My cat really hates dogs!"
local lower = string.lower(original)

for _, word in next, profanity do
	local i, j = string.find(lower, word)
	while i and j do
		original = string.sub(original, 1, i - 1) .. string.rep("#", j - i + 1) .. string.sub(original, j + 1)
		i, j = string.find(lower, word, j)
	end
end

print(original)

###Output:

>> My ### really hates ###s!

2 comments

0

I tried implementing this and it just didn't work at all. I'll edit with my current code.

0

Are the words in your censor list all lowercase? I didn't convert the variable word, so that could be the problem you are having. Try putting word = string.lower(word) right after the for loop.

This discussion is preserved as it was. New answers, comments, and votes are closed.