Search code examples
luagarrys-mod

gmod GameMode Lua. IsPlayer retuning nill value


I'm trying to make a gmod gamemode. In my init.lua I wanted it so that way team members can't hurt each other. So I used this code

function GM:EntityTakeDamage( target, dmginfo )
    if ( target:IsPlayer() and dmginfo:IsPlayer() ) then
        if (dmginfo:Team() == target:Team()) then
            dmginfo:ScaleDamage( 0.0 ) // Sets damage to 0
        end
    end
end

However it's giving me the error telling me that IsPlayer() is a nil value even though it should be returning a boolean. It points to no other lines other then the line with IsPlayer() and it's saying it is IsPlayer()


Solution

    1. you have a typo in line 3. dminfo
    2. You should narrow down which of your multiple IsPlayer() calls actually is nil
    3. dmgInfo is a CTakeDamageInfo which has no function IsPlayer()
    4. single line Lua comments are opened with --, not //

    https://wiki.garrysmod.com/page/Category:CTakeDamageInfo

    If you call a function and it says its nil, then check if it even exists. Or even better, check this befor you use the function in the first place.

    And to prevent you from coming back in a minute, CTtakeDamageInfo also does not have a function Team() as well.

    Check out CTDamageInfo:GetAttacker()