Could someone look at this code I made to stop swearing.


local function Noswear()
    Gm:PlayerSay(ply, text)
    if text == ("****") or ("****") or ("****") or ("*****") or ("*****") then
      return false
end


Are there any errors in the code? Would I do it like this:


hook.Add( "PlayerSay", "Noswear", function(ply, text, public)
	if text == ("****") or ("****") or ("****") or ("*****") or ("*****") then
		return false
	end
end)

Or instead of all the or’s, would I just put a bunch of elseifs? And would the noswear in the second code just be the function name? If so, how would I make that function local? Would I still just put local behind function (hook.Add( “PlayerSay”, “Noswear”,local function(ply, text, public))?

You’d want to use tables and you’ll also want to make the text lowercase that way people cant get around it by using capitals.

[lua]
local badWords = {
‘badword’ = true,
}

hook.Add( “PlayerSay”, “Noswear”, function(ply, text, public)
if badWords[text:lower()] then
return false
end
end)
[/lua]

You’d need to compare each with text… test == “x” || text == “y” etc…

Also, I’d say use string.find to search for results ( returns where the word starts and where it ends ) and string.lower ( to avoid needing to compare every possible way to write it as Lua is case sensitive and strings are too )…

fuck that

that’s not how logic works, essentially you’re doing
[lua]
if(text == ("****")) then
return false
end

if(("****")) then
return false
end

–etc
[/lua]
the proper way to do it would be
[lua]
if((text == “") or (text == "”) --[[etc]]) then
return false
end
[/lua]

although, a nicer way to do it would be to use tables

[lua]
local badwords = {
**** = true,
**** = true
}

hook.Add( “PlayerSay”, “Noswear”, function(ply, text, public)
if(badwords[text]) then
return false
end
end
[/lua]

also, instead of straight up comparing text, you should use

string.find

I’m probably going to use your code when I’m better with GLua.

Would this code work?



hook.Add( "PlayerSay", "noswear", function(ply, text, public)
  if badWords[text:lower()] then
	ply:PrintMessage(HUD_PRINTCENTER, "Please don't swear in chat!")
    return false
  end
end



hook.Add( "PlayerSay", "noswear", function(ply, text, public)
  for i = 1, #badWords do
    if string.find(text:lower(), badWords*) then
  	ply:PrintMessage(HUD_PRINTCENTER, "Please don't swear in chat!")
        return false
    end
  end
end

should probably also make it detect spaces around the phrase if you’re planning on using this on a server, since you probably don’t want to punish people who say snigger or hello, for example.

That also opens up the possibility for variants though e.g. “dumbf*ck” does’t get detected by the filter. It might be better to have a list of exceptions, or better yet, a list of words that block the chat in all cases and a list that are only blocked if they’re surrounded by at least one space.

I remember reading somewhere, that there’s a Pokemon which you can’t trade online, because its name sort of contains a swear word. The only way to trade it is to give it a custom name.

The lesson is: sometimes swearword filters can be really bad. Do watch out! :slight_smile:

Ok, I’ll give you the code for my chat control bot. Which refuses big words in chat commands and chat messages :



function ChatManager.ControlBot:ParseMessage(ply, msg)
	//Start blacklist check
	if (msg == nil or !msg) then return false end
	for k, v in pairs(ChatManager.ControlBot.WordBlacklist) do
		if (string.find(string.upper(msg), string.upper(v))) then
			if (!file.Exists("realistic_rp/chat_control_bot/", "DATA")) then
				file.CreateDir("realistic_rp/chat_control_bot/")
			end
			local dir = "realistic_rp/chat_control_bot/" .. ply:Name() .. ".txt"
			file.Append(dir, "Tried to use black listed word " .. v .. " in '" .. msg .. "'
") 
			return false
		end
	end
	return true
end
ChatManager.ControlBot.WordBlacklist = {
	"enculer",
	"enculé",
	"salop",
	"saloppe",
	"connard",
	"conard",
	"fdp",
	"fils de put",
	"put"
}


It’s at information title, only if you want to efficiently block big words from players.