Detecting for spam of letters in a string

Hi, basically what I want to know is how to find a certain amount of a letter in a string. At the moment I’m having issues with people just putting in ddddddddddddffgfdgddddddddddddd or something inside their ‘reason’ box, I don’t want to use chat filters as it would mess up other things inside my system (It goes to discord and the reason is not displayed in-game without appropriate filtering)

Thanks guys :smiley:

If you want to check how often a specific character repeats, you can just do local _, count = str:gsub("YourCharacterHere", ""). Repeating this for every character should work fine, but it might give bad results because it doesn’t check if the characters are in a row. For example, “vibrant igloo” would give 2 for i even though the is are definitely not right after each other.


If you want to check how many in a row there are, you could do a loop through every occurrence of multiple of the character and find the longest. That could look something like this:

local longest = 0
for i in str:gmatch("YourCharacterHere+") do
	-- the plus char is necessary, so it'd be something like `a+` or `b+`
	longest = math.max(longest, #i)
end

print(longest)

Repeating either of these methods for every alphabetical character would give you something along these lines:

local str = -- whatever your reason is idk

for i in ("abcdefghijklmnopqrstuvwxyz"):gmatch(".") do -- for every character in the alphabet,
	local longest = 0
	for i in str:gmatch(i .. "+") do -- find the longest number of repeats of that character
		longest = math.max(longest, #i) -- 
	end

	if longest > 3 then -- english should never have more than 3 of the same letter in a row
		-- so it's invalid
	else
		-- 3 and under means it's fine* (except this is really not a good way to check for spam)
	end
end

Keep in mind this won’t really keep out much spam, since they could just paste Lorem Ipsum or something.

4 Likes

Yes, I understand that but people don’t tend to do that. Most of the time, they want to get rid of the arrest reason box do they just hold their finger into a key and click submit.

1 Like

Yeah, that’s fine. I’m sure it’ll stop some people, just keep in mind it won’t block anyone who really cares.

3 Likes