When I use a cmd It does It for all players


hook.Add( "OnPlayerChat", "SHUD", function( ply, text )
   local Text = string.lower( text )
   if ( Text == "!showhud" or Text == "/showhud" ) then
      RunConsoleCommand( "cl_hidehud", "0" )
		 chat.AddText(Color(255,255,255),"[",Color(200,72,200),"Timer",Color(255,255,255),"] HUD enabled.")
      return true
   end
end )

When I do !showhud the command works for every player I want it to work just for the person who typed it any help?

use ply:ConCommand() instead of RunConsoleCommand.

Ah I tried that and nothing happens.

do you have hidehud set to 1?


if ply == LocalPlayer() and ( Text == "!showhud" or Text == "/showhud" ) then

I’ll test that later does anyone know why this dosen’t work?


hook.Add( "OnPlayerChat", "Test", function( ply, text )
   local Text = string.lower( text )
     if ply == LocalPlayer() and  ( Text == "!test" or Text == "/test" ) then
       RunConsoleCommand( 'say', "This is a test!" )
      return true
   end
end )

Use printing to debug and see where its failing.
BTW where are you putting it?

In my gamemode, cl_init.lua

Don’t do RunConsoleCommand on a LocalPlayer, just don’t.


hook.Add( "OnPlayerChat", "Test", function( ply, text )
   local Text = string.lower( text )
     if ( Text == "!test" or Text == "/test" ) then
       LocalPlayer():ConCommand( 'say "This is a test!"' )
      return true
   end
end )

I tried that and it didn’t work but it works for other things just not the say why is this?

[editline]30th July 2015[/editline]

It makes no sense because I tried this and this worked:


local function Test()
	LocalPlayer():ConCommand("say testestest")
end
timer.Create("Testing", 2, 0, Testing)

bump

if i’m correct it shouldn’t work on every player because OnPlayerChat is a clientside hook and RunConsoleCommand would just be ran on that client?

   LocalPlayer():ConCommand( 'say "This is a test!"' )

I mean this code doesn’t work only in OnPlayerChat why is this?

Why dont you use the serverside hook instead, and then try “ply:ConCommand”?

What? I’m trying to get this to work:


hook.Add( "OnPlayerChat", "Test", function( ply, text )
   local Text = string.lower( text )
     if ( Text == "!test" or Text == "/test" ) then
       LocalPlayer():ConCommand( 'say "This is a test!"' )
      return true
   end
end )

I wouldn’t even go like that. Why find !test anywhere in the text when you could check for if they actually wanted to say the command?


hook.Add( "OnPlayerChat", "ThisCommandIsATest", function( ply, text, public )
     if string.lower( text, 1, 5 ) == "!test" or string.lower( text, 1, 5 ) == "/test" then
       LocalPlayer():ConCommand( "say This is a test!" )
      return true
   end
end )

Also this

It’s just simpler

It’s actually not, plus it depends on what you need it for.

Here’s a client side example
[lua]
hook.Add(‘OnPlayerChat’, ‘testcommands’, function(ply, text)
if (ply == LocalPlayer() and string.match(string.lower(text), ‘^[!/#@]test’)) then //make sure WE ran it. check if the command is !test /test #test or @test
RunConsoleCommand(‘say’, ‘this is a test’) //do the stuff
end
end)
[/lua]

and a server side version

[lua]
hook.Add(‘PlayerSay’, ‘testcommands’, function(ply, text)
if (string.match(string.lower(text), ‘^[!/#@]test’)) then //check if the command is !test /test #test or @test
ply:ConCommand(‘say’, ‘this is a test’) //do the stuff
end
end)
[/lua]

Serverside you don’t need to check if it’s you who ran it.