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!
[CODE]
hook.Add( "OnPlayerChat", "TestCommand", function( ply, txt )
txt = string.lower(txt)
if ( txt == "test" ) then
print("Debugging")
RunConsoleCommand( "say", "test" )
return true
end
end )
[/CODE]
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: [url]http://wiki.garrysmod.com/page/concommand/Add[/url]
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
[CODE]
RunConsoleCommand( "say", "test" )
[/CODE]
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
[CODE]RunConsoleCommand( "kill" )[/CODE]
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
[CODE]
RunConsoleCommand( "say", "ALTERNATE TEXT" )
[/CODE]
[QUOTE=MPan1;50788904]It might just be printing 'test' at the same time that it's cancelling out the 'test' text. Try changing it to
[CODE]
RunConsoleCommand( "say", "ALTERNATE TEXT" )
[/CODE][/QUOTE]
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]
[CODE]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 )[/CODE]
and one more thing, if you wan't to run a console command its:
[CODE]ply:ConCommand( "kill" )[/CODE]
[QUOTE=jacobcooper18;50788927]Can't he just use ply:Say( "hi" ) that will make the player automatically say Hi.
And it will reduce heaps of errors[/QUOTE]
That's pretty much the same thing as RunConsoleCommand with 'say' as the command
[B]EDIT: [/B]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
[QUOTE=MPan1;50788958]That's pretty much the same thing as RunConsoleCommand with 'say' as the command[/QUOTE]
true but it is easier to do.
Sorry, you need to Log In to post a reply to this thread.