r/robloxgamedev • • 10h ago

Help Me With Coding Filtering randomly generated text

I'm an amateur scripter, and I have this system that creates a 4-letter string of random characters, using only lowercase letters and numbers, but I am concerned over the possibility that it could generate a swear word. It's unlikely but I want to make sure this doesn't happen. I don't really want to use Roblox's built-in filtering, as the strings need to be generated instantly for each round, and it can cause other issues. What should I do?

2 Upvotes

3 comments sorted by

3

u/Dry-Lingonberry-388 10h ago

TextService:FilterStringAsync()

1

u/Massive_Fondant7439 5h ago

You are right that FilterStringAsync is the wrong tool here. It is async, it needs a real player to filter against, and it exists for text a player typed, not for text your game generated. For a four character code you only need to catch a handful of words, so do it locally.

Three cheap layers, in this order:

  1. Denylist with reroll. Keep a table of the bad 3 and 4 letter strings and check the generated code against it. Checking four characters costs microseconds, so just reroll until it passes. Players will never notice.
  2. Normalize before you check. Map 1 to i, 0 to o, 3 to e, 4 to a, 5 to s, 7 to t and 8 to b, then run the same check again. That covers l33t output without keeping a second list.
  3. Do not generate free letters at all. Use a pattern like consonant vowel consonant vowel from a curated set, or a pool of two character syllables. Codes still look random, they are pronounceable, and the chance of landing near a real word drops to almost nothing.

You still want the built in filter on anything a player can type, chat and signs and display names, because that is what it is actually for. Codes your system generates just need the local check.

1

u/EducatorOne7130 1h ago

The simplest trick (used by a lot of invite-code generators) is to just remove vowels from your character pool—and usually 0/1/l too so players don't misread them:

local CHARS = "bcdfghjkmnpqrstvwxz23456789"

Without a, e, i, o, u, y, it's practically impossible to spell English swear words, there's zero async delay, and you don't have to maintain a denylist. With 27 chars you still get 27^4 = 531,441 unique 4-character codes, which is plenty for per-round IDs. (If you still want to be extra safe against leetspeak like fck / sht / cnt, just skip a tiny 5–10 entry blocklist on top.)