I am trying to make a custom chat and I want a word blacklist (words that can't be said so they get replaced with #'s), I have tried and it worked but when it did it it also changed the whole message's case and I couldn't see any way around this. This is an example of what I did:
v = "LOL" msg = "Hi, I like to LOL because I can" print(string.gsub(string.lower(msg),string.lower(v),string.rep("#",string.len(v)))) -- The output is: hi, i like to ### because i can
My problem is that it changes the whole message to lower case and if I try to fix it by not changing the message to lower case this is the script I could use:
v = "lol" msg = "Hi, I like to LOL because I can" print(string.gsub(msg,string.lower(v),string.rep("#",string.len(v)))) -- The output is: Hi, I like to LOL because I can
My problem here is that if the message has the word but in a different case and the only way I can think of fixing this is having a table of all the possible cases but if I do a long word like intermediate it has 100's of possibilities and that's just one word. If you know a quick way to do this please help me!
This script should work to sum up what it does, it converts the string to lower text then removes all of the blocked text then compares the processed text with the users message and converts the changes.
I have also added hashes for the text length so it will increase and decrease with the text length.
I will add a comments and explain it if you need me too, as I don't have much spare time atm.
local blkList = {'lol','ok'} --All needs to be in lower case, dont worry it will be converted to upper case later :P local message = "Hallo lol ok Lol" function check(msg) local lowMsg = string.lower(msg) local procMsg = lowMsg for i, v in pairs(blkList) do for w in string.gmatch(lowMsg, string.lower(v)) do local len = string.len(w) local txt = string.rep("#", len) procMsg = string.gsub(procMsg, v, txt) end end local outStr = "" for i = 1, string.len(msg) do if string.sub(procMsg, i,i) == "#" then outStr = outStr .. string.sub(procMsg, i,i) else outStr = outStr .. string.sub(msg, i, i) end end return outStr end print(check(message))