dragon3025 wrote:Enjl wrote:You have to wrap killedNPC and start doing stuff with it in a scope where it's actually defined. I suggest after the killedNPC.id check. Also I noticed that your HP only counts down on killReason 9 which is equivalent to despawn or being collected.
You posted eariler to make sure to also allow death by reason 9 if you want despawning to be possible. I was using 3 to make it so stomping on it 3 times kills it, did you mean use 9 along with 3? My code is now:
Code: Select all
local pnpc = loadSharedAPI("pnpc")
function onNPCKill(killObj, killedNPC, killReason)
w = pnpc.wrap(killedNPC)
if w.data.hp == nil then
w.data.hp = 0
end
if killedNPC.id == 168 then
if killReason == 9 then
if w.data.hp < 2 then
w.data.hp = w.data.hp + 1
else
killObj.cancelled=true
end
end
end
end
I didn't get any errors, but I still couldn't kill it by jumping on it and hitting it with a hammer killed it in one hit.
Sorry for confusing you with the killReason==9 thing earlier, time to get this resolved.
The check "if killReason == 9 then" makes it so that everything until the "end" which closes the condition will only run when an enemy is despawned or collected. Your code checks for all despawning npc with ID 168 and executes the code for those.
My previous mention of killReason == 9 was to cover an edge-case where your NPC would be immortal when trying to despawn. Typing this made me remember another edge-case, lava.
So what you want is to not execute ANY code on your NPC if it dies by lava or despawn (killReasons 6 and 9). Furthermore, killReason 1 is the default kill. Reason 3's "squish" refers to NPCs getting crushed by moving layers.
All this condition jumble can look like this:
Code: Select all
if killReason == 1 and w.data.hp < 2 then
w.data.hp = w.data.hp + 1
else
if killReason ~= 6 and killReason ~= 9 and w.data.hp < 2 then --don't kill the NPC unless it touches lava, despawns or was bonked three times.
killObj.cancelled = true
end
end
Your NPC will, in this case, still be vulnerable to other types of harm, such as tail swipe, spinjump or shells. To make your NPC immune to them, all you need is to expand your killReason checks. If you want to check for death by specific npcs, things get more complicated.