Healing by killing

Can anyone make this proof-of-concept script to work ?

Im still learning Lua but made the script to work when it didn’t used userdata/cvar value

This state currently crashes with



Hook 'NPCsTakeDamage' Failed: damage_script.lua:18: attempt to compare number with userdata


[lua]
dls_mul_num = CreateConVar(“dls_damage_multiplier”, “0.2”, { FCVAR_ARCHIVE, FCVAR_NOTIFY } )

function EntTakeDamage( ent, inflictor, attacker, amount, dmginfo )

if (!ent:IsNPC() or !attacker:IsPlayer() or amount < 1) then return end

local CheatsOn = server_settings.Bool( "sv_cheats", 0 )
local dls_hitpoints = attacker:Health()
dls_mul_num = GetConVar("dls_damage_multiplier"):GetFloat()

if ( amount > ent:GetMaxHealth() ) then
	local dls_damage_amount = ent:GetMaxHealth()
end
if ( amount <= ent:GetMaxHealth() ) then
	local dls_damage_amount = amount
end
			
	 if ( dls_multiplier >= 0 and dls_multiplier <= 1 ) then
		local dls_hp_amount = dls_hitpoints + math.Round( dls_damageamount * dls_mul_num )
	 end
	 if ( dls_multiplier >= 0 and CheatsOn == 1 ) then
		local dls_hp_amount = dls_hitpoints + math.Round( dls_damageamount * dls_mul_num )
	 end			
	
attacker:SetHealth(dls_hp_amount)

end

hook.Add(“EntityTakeDamage”, “NPCsTakeDamage”, EntTakeDamage)
[/lua]

You should be using dls_mul_num instead of dls_multiplier in your conditions.

I get the same error.
And dls_multiplier was because I changed locals few times…
So I need to get userdata to be a number

Here you go :
[lua]CreateConVar(“dls_damage_multiplier”, “0.2”, { FCVAR_ARCHIVE, FCVAR_NOTIFY } )

hook.Add(“EntityTakeDamage”, “NPCsTakeDamage”,function( ent, inflictor, attacker, amount, dmginfo )
–The user never sees the names you use here so no need to prefix local variables with dls_. Use clear names.
local fMultiplier = GetConVar(“dls_damage_multiplier”):GetFloat()
if !(fMultiplier > 0) or !ent:IsNPC() or !attacker:IsPlayer() then return end – Don’t bother running the rest if the multiplier is 0

local bCheatsOn = server_settings.Bool( "sv_cheats", 0 )
if not bCheatsOn then fMultiplier = math.Clamp(fMultiplier,0,1) end -- Unless cheats are on, clamp the value of fMultiplier
        
local iDamage = math.Min(amount,ent:GetMaxHealth()) --iDamage will be amount unless the victim's max health is lower.
local iAttackerHealth = attacker:Health() + math.Round( iDamage * fMultiplier )

attacker:SetHealth(iAttackerHealth)

end)[/lua]
:eng101:

Thanks!

I’m so far from being good at Lua :frown:
Also - I found a bug :v:
If you shoot Rollermines or turrets you still get health

Will this actually work on other server, such as DarkRP?

Well that’s not a bug since they’re NPCs. :smile:
Just make a list of forbidden NPCs and check against it, or a white list if you want.

[editline]11:22AM[/editline]

Yes, it will work anywhere.