Need help with some code

Hello, I’m fairly new to coding in lua and coding overall. I’ve decided to make a script that works sort of like ulx I suppose just on a much more dumbed down scale and will only work with console commands though the code I’ve slapped together isn’t working and I’m dumbfounded as to why it’s not working. Any assistance would be greatly appreciated!



hook.Add( "OnPlayerChat", "TestCommand", function( ply, txt )

    txt = string.lower(txt)
	
	if ( txt == "test" ) then 
		print("Debugging") 
		RunConsoleCommand( "say", "test" ) 
		return true 
	end
end )


Where are you running this? Also, remember to check ply is LocalPlayer() or every player on the server will say ‘test’ since this hook gets run whenever anyone chats

You said that you wanted to use a console command? This is the code for a chat command, and it will only run when you type “test” in chat. If you wanted to create a console command you would use: http://wiki.garrysmod.com/page/concommand/Add
The examples there are also very good.

I’ve been running the lua client side on a friend’s server so I didn’t see there being a problem of it affecting everyone, I’ll check out the link for concommand.Add thanks!

Also, one question. Did it say “test” in chat when you typed test or not? If it did then the chat command worked.

That’s the problem I’m having, it’s not saying “test” in chat.

Maybe the problem is something to do with



RunConsoleCommand( "say", "test" ) 


Being in a hook checking if the player says test- maybe it’s creating an infinite loop or just printing ‘test’ again as it would if you typed it normally so you aren’t noticing?

I changed the code to this


RunConsoleCommand( "kill" )

and it’s both printing “Debugging” and killing me so it seems to be just the say command isn’t working

It might just be printing ‘test’ at the same time that it’s cancelling out the ‘test’ text. Try changing it to



RunConsoleCommand( "say", "ALTERNATE TEXT" )


Can’t he just use ply:Say( “hi” ) that will make the player automatically say Hi.

And it will reduce heaps of errors…

[editline]28th July 2016[/editline]


hook.Add( "OnPlayerChat", "TestCommand", function( ply, txt )
	
	if ( string.sub( 1, 5 ) == "test" ) then -- String.sub reads better, and just tests for test.
		ply:Say( "Well this works..." ) -- What player says..
		return true -- Hides what the player just typed...
	end

end )

and one more thing, if you wan’t to run a console command its:


ply:ConCommand( "kill" )

That’s pretty much the same thing as RunConsoleCommand with ‘say’ as the command

**EDIT: **I’d say they’re both about the same level of easiness- but ply:Say looks a bit better when you’re writing it down I guess

true but it is easier to do.