Run command when taken damage

Is there a possible way, that I can make my clientside run a command when I take damage. I.e. “say /holster”, when someone starts shooting me.

I was thinking I could do sort of an If, else thing with OnTakeDamage but im not too sure on how to go about that.

Thanks in advance

Use the EntityTakeDamage hook.


function GM:EntityTakeDamage( target, dmginfo )
	
	if ( target:IsPlayer() and dmginfo:IsBulletDamage() ) then

		LocalPlayer():ConCommand("say /holster")

	end

end


Ive gotten this far, im not too sure on how to make it work though.

Also i’ve been thinking it might be more like this:


hook.Add("holsterscript", function()
	if ( target:IsPlayer() and dmginfo:IsBulletDamage() ) then

		LocalPlayer():ConCommand("say /holster")
		end
	end
end)

Neither have worked so far

You aren’t using hooks correctly. Read the examples here: http://wiki.garrysmod.com/page/hook/Add

So, in the same script i’d just need to add a function basically doing what I need, then calling it with hook?

[editline]17th August 2017[/editline]

I’ve revised it a little bit and think im close:



hook.Add("holsterscript", "EntityTakeDamage", function()

local function GM:EntityTakeDamage( target, dmginfo )
	
	if ( target:IsPlayer() and dmginfo:IsBulletDamage() ) then

		LocalPlayer():ConCommand("say /holster")

	end

end

Edit: I get this error:



[ERROR] lua/holsterscript.lua:3: '(' expected near ':'
  1. unknown - lua/holsterscript.lua:0

EDIT Again: I’ve gotten to a point with 0 errors, i don’t think its working however.


hook.Add("EntityTakeDamage", "holsterscript", HolsterScript)

local function HolsterScript( target, dmginfo )
	
	if ( target:IsPlayer() and dmginfo:IsBulletDamage() ) then

		LocalPlayer():ConCommand("say /holster")

	end

end

Although there arent any errors, it doesnt even say “test” instead of /holster

Edit 3: Ive changed it yet again, no errors, but no print


hook.Add("EntityTakeDamage", "holsterscript", HolsterScript)

local function HolsterScript( target, dmginfo )
	
	if ( target:IsPlayer() and dmginfo:IsBulletDamage() ) then

		Player:PrintMessage( HUD_PRINTTALK, "Im testing code." )
	end

end

Lost at this point. Any help would be nice.

Try


target:ConCommand("say /holster")

I put in test instead of holster, still doesn’t work.

[editline]17th August 2017[/editline]

The function EntityTakeDamage is a server side function, that could be the root of the issue. Anyone have something thats clientside?

[lua]
local function HolsterScript( target, dmginfo )
if ( target:IsPlayer() and dmginfo:IsBulletDamage() ) then
target:PrintMessage( HUD_PRINTTALK, “Im testing code.” )
end
end

hook.Add(“EntityTakeDamage”, “holsterscript”, HolsterScript)
[/lua]

Like this?


   function GM:EntityTakeDamage( target, dmginfo )
	
	if ( target:IsPlayer() and dmginfo:IsBulletDamage() ) then

		target:ConCommand("say /holster")

	end

end   

Edit: Ninja’d

With yours theres no error but it doesnt say anything in chat

Are you running it Serverside or Client?

Yours gives me this error:


[ERROR] lua/holsterscript.lua:1: attempt to index global 'GM' (a nil value)
  1. unknown - lua/holsterscript.lua:1


Neither of them say anything when i take dmg

[editline]17th August 2017[/editline]

Clientside

Put in init.lua, it’s a server side hook

EntityTakeDamage is serverside.

That and printing messages on a client is also serverside, client if the client is the calling target.

Im making this for clientside wouldn’t i need a server, to put it in init.lua?

While testing put it into a file inside or autorun/server

In the gmod folder autorun/server, you want me to make an init.lua file? Im not following you here.

Name the file what ever you want testing.lua even.

Then put that code that I posted earlier into the file and it should work.

Alright, but how do i run the script when in singleplayer?

It should run by itself since it’s in autorun.