• Run function on damage from zombie
    5 replies, posted
Hey, facepunch. I have nearly zero experience in LUA coding, so I would like some assistance. [CODE]function GM:EntityTakeDamage( ent, inflictor, attacker, amount ) print("checking for player") if ( ent:IsPlayer() ) then print("checking for zombie") if(attacker:GetModel() == "models/Zombies/Classic") then print("Zombie detected") MyFunction(ent) end end end [/CODE]Wouldn't this run the function "MyFunction" with ent, when a player is damaged by a zombie? I can't get it to work, and none of the prints are happening. The lua is located in lua/autorun/server/ Thank you.
[lua] hook.Add("PlayerHurt","WhateverYouWantToCallThisHook", function(p,zombie) if(zombie:GetClass()=="npc_zombie") then --Dostuff end end) [/lua]
Defining the GM: functions should only occur in gamemode scripts. Since you're making an autorun script, add a hook to EntityTakeDamage: [lua] local function CheckZombie(ent, inflictor, attacker, amount) print("checking for player") if ( ent:IsPlayer() ) then print("checking for zombie") if(attacker:GetModel() == "models/Zombies/Classic") then print("Zombie detected") function (ent) end end end hook.Add("EntityTakeDamage", "CheckZombieDamage", CheckZombie) [/lua] You also can't have a function called function. Function is a reserved word in the language. And I'd check for the zombie classname rather than the model of the NPC. [lua] local function CheckZombie(ent, inflictor, attacker, amount) print("checking for player") if ( ent:IsPlayer() ) then print("checking for zombie") if(attacker:GetClass() == "npc_zombie") then print("Zombie detected") --other code end end end hook.Add("EntityTakeDamage", "CheckZombieDamage", CheckZombie) [/lua]
[QUOTE=Ronon Dex;24647308]hook.Add("PlayerHurt","WhateverYouWantToCallThisHook", function(p,zombie) if(zombie:GetClass()=="npc_zombie") then --Dostuff end end) [/QUOTE] Thank you very much.
Ide rather use PlayerHurt, Since its a less demanding hook.
[QUOTE=Wizey!;24650977]Ide rather use PlayerHurt, Since its a less demanding hook.[/QUOTE] Incase you didn't notice - Ronon did use PlayerHurt.
Sorry, you need to Log In to post a reply to this thread.