Catching a kill

How can i detect if a player kills a entity (npc or other player)?
getting the difference between them isn’t the problem :slight_smile: just getting a reference to the entity

is it also possible to differentiate between npcs?
i have some “special” spawns that are just a bit stronger. is it possible to give them values? (like special name so it can be displayed in chat, a variable which denotes that it is a special spawn)


For players


For NPCs

i really thought it would be a custom function, well can’t complain can we :smiley:

but about the “special” npc, no way to detect that? and no way to set a name to that npc?
the plan for this was to have the special npcs to be stronger and differently colored (no problem with that) but when he dies i wanted to display a chat message with his name in it :slight_smile:

You can set a variable on the Special NPC to true when you spawn it, just like setting a variable on a table. Then in the NPC death hook, you can check if this variable is true. If it is, then it is a special NPC.

but how do i do that exactly? that is the problem :stuck_out_tongue:
the idea behind it (having a flag on the npc) wasn’t the problem
i thought about checking the color, if it was different from 0, 0, 0 then it is special :stuck_out_tongue:

but i guess, if you can set the variable to true on a npc, then likewise you can set a variable with the name on the npc :smiley:

but still, how? xD

You can set variables on entities just like you do on tables.

[lua]ent.Variable = true[/lua]


[lua]ent.Name = “Some special name”[/lua]


I don’t think I am understanding what you actually need help with.

nothing anymore :stuck_out_tongue:
in fact, i was thinking it would be like that, but then i tought “thats too obvious”
LUA is showing it strong points as scripting :smiley:


Ok one more thing :stuck_out_tongue:
i try to set the health on my special npc using both ent:SetHealth(x) and ent:SetMaxHealth(x) but it doesn’t seem to change at all :frowning:
i have set it to 1 as a test but he still spawns with 40 hp

Post the code. :slight_smile:

right :stuck_out_tongue: forgot about that xD (reinstalled windows so a bit busy, but its mostly done :3)
[lua]function SpawnSpecialNPC(player, command, args)
local ent = ents.Create(args)
ent:SetColor(255, 0, 0, 255)
ent.Name = “Metrocop” … tostring(math.random(0, 9999))
ent.IsSpecial = true
ent:SetPos(Vector(0, 0, 0))
error("Special NPC Spawned with " … ent:Health() … "hp.
concommand.Add(“rpg_dev_specialspawn”, SpawnSpecialNPC)[/lua]

NOTE that this us mainly a test function :slight_smile: