EntityTakeDamage takes armor !

Sorry for disturbing you, but I’ve a little problem. When I use the EntityTakeDamage function and I ScaleDamage( 0 ), HP aren’t lost, but armor are. I would like to find a way to not lost armor. I tried to transform that :

function GM:EntityTakeDamage( target, dmginfo )

if target:IsPlayer() then

		dmginfo:ScaleDamage( 0 )



into that :

function GM:EntityTakeDamage( target, dmginfo )
local armor = target:Armor()
if target:IsPlayer() then
		dmginfo:ScaleDamage( 0 )
                target:SetArmor( armor )

But I know that’s not a good way and that doesn’t work.
Please help is welcome!
Thanks by advance :slight_smile: !


function GM:EntityTakeDamage( target, dmginfo )

	if target:IsPlayer() then

		local plyHealth = target:Health()
		local damage = dmginfo:GetDamage()

		target:SetHealth( plyHealth - damage )



Above code is improper.Take a look at the wiki for damageinfo.

Basically just return true within the hook function to have no damage taken

Whats wrong with my code ? It works fine and it does what he asked.

Except it’s taking away the player’s damage two times

[editline]2nd May 2016[/editline]

You’re not meant to SET the player’s health using that function, you’re meant to EDIT the dmginfo table provided by the function and return it

[editline]2nd May 2016[/editline]

What you’re doing there is literally getting the damage that is about to be inflicted, then taking that damage away from the player’s health, but you never changed the dmginfo table or returned anything (which is the entire point of using that function), so it will not only take away the player’s health, but their armor as well.

Thanks. That works and that what I’m waiting.
I’ve just one answer :
Why in functions you must return true or false at the end of a condition (if in this example), and here just I’ve to return true simply ?

Don’t know if it works and does what I asked, but by logic I don’t know how your script works. I would like to keep the armor, and here you decrease health of the target?

Anyway, thanks for all people who help me!

Functions do things, they can return whatever they want. The reference for garrysmod simply lists what to expect as a return from all the built in function, a made up standard.
Some built in functions will do call hooks (which just means it alerts the code environment that this function has just ran) these will look like :

hook.Call("SomeStringHere",nil,"test") -- the nil is for gamemode table, it is not a variable to pass.

When the previous code is executed, it will trigger anything that does this:

hook.Add("SomeStringHere","MyUniqueNameThatCanBeHookedAlso",text) --the text variable will be "test"

The functions that do a hook.Call are generally meant not to be overriden, they do basic game functionality stuff. Having a function use a hook.Call, allows the user to then do hook.Add, and “append code” to the end of that function without removing the basic functionality that is needed.
Usually these hook.Call’s are executed BEFORE a function returns a value to whatever called it. After a hook.Call has been executed, and a user “appends some extra code” to the function with a hook.Add, the function continues to run the line after hook.Call and does some logic before doing a function return. As mentioned earlier the wiki has listed what is to be expected from these functions, so you can force the function to “return” different values based on what you did in the hook.Add.

An example would be:

hook.Add("EntityTakeDamage","ThisIsMyUniqueID", function(target,damageinfo) 
      --target is, what is taking the damage.
     --damageinfo is a table that is passed, containing damage information like: type,how much damage...etc
     -- If you dont return anything here, the function that calls "EntityTakeDamage" will proceed as normal, applying the damage in the damageinfo to the target player.
     -- if you return true here, the function that calls "EntityTakeDamage" will recognise this and smiply terminate without applying any damage.

    if(target:IsPlayer()) then
          return true  --Wiki says if we return true, damage will NOT be applied
         return false  -- If its not a player, damage it!

All that being said, If you know what you are doing you can override things to make them customized, although most functions do a lot more in the background than the hook.Calls make it seem.
for example:

--If you make a custom gamemode, you could override the PlayerInitialSpawn function.
function GM:PlayerInitialSpawn(ply)
     --Since we have overriden the built in function. We will need to do some "low level" things here, that are not needed to be done when using a hook.

    --For example, you wil need to set player classes, setup data tables, set models of the player, draw clientside hands.

Note- Don’t hate on me for abstracting away from the technical details…I know I know… :smile:

Thanks for this descriptive and impressive answer! I think that now I understand return of functions :stuck_out_tongue:
(but fortunately it is written if there is a specific return in a function).

Thanks, have a good day/night!