1. Post #1
    Ningaglio's Avatar
    February 2009
    938 Posts
    this is my code, for some reason, it doesnt work

    File in autorun/server/chat_filter.lua

    Code:
    hook.Add("PlayerSay", "chat_filter", function( player, strText )
    
    	local tab = {}
    	
     
    	if ( IsValid( player ) ) then
    		table.insert( tab, player )
    	else
    		table.insert( tab, "Console" )
    	end
    	
    	local incomplete = string.Explode( strText, " ")
    	local pre_table = util.KeyValuesToTable( file.Read( "meisno/reham.txt" ) )
    	
    	local function apartk()
    		for k,v in ipairs(pre_table) do
    			return k
    		end
    	end
    	
    	local function apartv( K )
    		for k,v in ipairs(pre_table) do
    			if k == K then
    				return v
    			end
    		end
    	end
    	
    	for k,v in ipairs(incomplete) do
    		local value = tostring(v)
    		if value == apartK() then
    			value = apartv( k )
    		end
    	end
    	
    	local complete = string.Implode( incomplete , " " )
     
    	table.insert( tab, Color( 255, 255, 255 ) )
    	table.insert( tab, ": "..complete )
     
    	chat.AddText( unpack(tab) )
     
    	return true
     
    end)
    Code in data/meisno/reham.txt

    Code:
    "CHAT_FILTER"
    {
    	"fuck"		"F***"
    	"shit"		"S***"
    	"GMT"		"Narb"
    }

  2. Post #2
    Gold Member
    nicatronTg's Avatar
    July 2009
    4,704 Posts
    Test it in a listen server first, it's got to be erroring somewhere or not functioning. Server errors aren't visible unless you host the server or you are the server.

    Next, I somehow remember that this conflicts with commands, but oh well. Anyhow, someone wrote a censor for me a while back.

    Also, use [lua] tags not [code] tags in the future.

  3. Post #3
    open.gl
    Overv's Avatar
    February 2007
    7,431 Posts
    That is not how you would use PlayerSay to alter the text. If you want to filter the word 'fuck' in chat text, you would do this:
    hook.Add( "PlayerSay", "Filter", function( ply, txt )
        return string.replace( txt, "fuck", "****" )
    end )
    You were trying to needlessly call chat.AddText, which does not exist serverside.

  4. Post #4
    H0rsey's Avatar
    March 2010
    227 Posts
    Are you sure to use string.replace?

  5. Post #5
    Gold Member

    April 2009
    726 Posts
    Are you sure to use string.replace?
    Yes he is. He just forgot to capitalize it
    hook.Add( "PlayerSay", "Filter", function( ply, txt )  
        return string.Replace( txt, "fuck", "****" )  
    end ) 
    

  6. Post #6
    Taught by John Lua
    MakeR's Avatar
    May 2007
    2,914 Posts
    Use String.gsub instead, it is faster.

  7. Post #7
    H0rsey's Avatar
    March 2010
    227 Posts
    ^

    Yeah, that's what I meant.