• Run command when taken damage
    20 replies, posted
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.
[CODE]function GM:EntityTakeDamage( target, dmginfo ) if ( target:IsPlayer() and dmginfo:IsBulletDamage() ) then LocalPlayer():ConCommand("say /holster") end end [/CODE] 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: [CODE]hook.Add("holsterscript", function() if ( target:IsPlayer() and dmginfo:IsBulletDamage() ) then LocalPlayer():ConCommand("say /holster") end end end)[/CODE] Neither have worked so far
You aren't using hooks correctly. Read the examples here: [url]http://wiki.garrysmod.com/page/hook/Add[/url]
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: [CODE] hook.Add("holsterscript", "EntityTakeDamage", function() local function GM:EntityTakeDamage( target, dmginfo ) if ( target:IsPlayer() and dmginfo:IsBulletDamage() ) then LocalPlayer():ConCommand("say /holster") end end[/CODE] Edit: I get this error: [CODE] [ERROR] lua/holsterscript.lua:3: '(' expected near ':' 1. unknown - lua/holsterscript.lua:0[/CODE] EDIT Again: I've gotten to a point with 0 errors, i don't think its working however. [CODE]hook.Add("EntityTakeDamage", "holsterscript", HolsterScript) local function HolsterScript( target, dmginfo ) if ( target:IsPlayer() and dmginfo:IsBulletDamage() ) then LocalPlayer():ConCommand("say /holster") end end[/CODE] 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 [CODE]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[/CODE] Lost at this point. Any help would be nice.
Try [CODE]target:ConCommand("say /holster")[/CODE]
[QUOTE=xDoomx;52585561]Try [CODE]target:ConCommand("say /holster")[/CODE][/QUOTE] 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? [CODE] function GM:EntityTakeDamage( target, dmginfo ) if ( target:IsPlayer() and dmginfo:IsBulletDamage() ) then target:ConCommand("say /holster") end end [/CODE] Edit: Ninja'd
[QUOTE=MarZ333;52585585][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][/QUOTE] With yours theres no error but it doesnt say anything in chat
Are you running it Serverside or Client?
[QUOTE=xDoomx;52585588]Like this? [CODE] function GM:EntityTakeDamage( target, dmginfo ) if ( target:IsPlayer() and dmginfo:IsBulletDamage() ) then target:ConCommand("say /holster") end end [/CODE] Edit: Ninja'd[/QUOTE] Yours gives me this error: [CODE][ERROR] lua/holsterscript.lua:1: attempt to index global 'GM' (a nil value) 1. unknown - lua/holsterscript.lua:1 [/CODE] Neither of them say anything when i take dmg [editline]17th August 2017[/editline] [QUOTE=MarZ333;52585596]Are you running it Serverside or Client?[/QUOTE] 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.
[QUOTE=xDoomx;52585607]Put in init.lua, it's a server side hook[/QUOTE] 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
[QUOTE=MarZ333;52585620]While testing put it into a file inside or autorun/server[/QUOTE] 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.
[QUOTE=MarZ333;52585628]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.[/QUOTE] Alright, but how do i run the script when in singleplayer?
It should run by itself since it's in autorun.
[QUOTE=MarZ333;52585632]It should run by itself since it's in autorun.[/QUOTE] Alright, its in there and everything, still doesn't work even when changed to say test. [editline]17th August 2017[/editline] Copied it wrong, it DOES work [editline]17th August 2017[/editline] But the thing is, I'm looking to run this on a client on a different server. Such as the lua_openscript_cl command [editline]17th August 2017[/editline] And, this types /holster in like server chat almost like the print command, not in chat for darkrp etc. [editline]17th August 2017[/editline] Anyone got a fix? [editline]17th August 2017[/editline] [CODE]local function HolsterScript( target, dmginfo ) if ( target:IsPlayer() and dmginfo:IsBulletDamage() ) then target:PrintMessage( HUD_PRINTCONSOLE, "say hi" ) end end hook.Add("PlayerTraceAttack", "holsterscript", HolsterScript)[/CODE] Tried using PlayerTraceAttack, cant seem to get it to work if anyone has a fix or they see an error.
Sorry, you need to Log In to post a reply to this thread.