Include method "Team"

Hi, i would like to include the method “Team” in my script but it alaways say “attempt to call a method ‘Team’ (a nil value)”

Please help :confused:

Make sure the player object you’re referencing IsValid…

Here is an example if you want to use it in a hud… You need to ensure LocalPlayer( ) IsValid before using LocalPlayer( ):Team( ); because LocalPlayer( ) can be NULL / nil ( read as invalid ) for a few frames while the player is connecting. The same can be true with what you’re doing.

Show us your code.

Here are some examples ( these aren’t meant to be an exact answer, but it shows how to ensure your player object is valid, etc… ):

https://dl.dropboxusercontent.com/u/26074909/tutoring/vgui/proper_hud_creation.lua.html

And some “simple” tuts on familiarizing yourself with the language:

https://dl.dropboxusercontent.com/u/26074909/tutoring/strings/string_concatenation.lua.html
https://dl.dropboxusercontent.com/u/26074909/tutoring/tables/quick_intro_to_tables.lua.html
https://dl.dropboxusercontent.com/u/26074909/tutoring/logic/ternary_operations.lua.html

Ok, i think it work

[editline]1st December 2014[/editline]

No it don’t work : here the code




function EntityTakeDamage( ply, dmginfo )
ply = LocalPlayer()

	 if ((ply:IsPlayer() and dmginfo:IsBulletDamage())and ply:Team() == TEAM_PC_RECRUE or ply:Team() == TEAM_PC or ply:Team() == TEAM_PC_MEDIC or ply:Team() == TEAM_PC_ELITE or ply:Team() == TEAM_PC_INTERVENTION or ply:Team() == TEAM_PC_OFC ) then
	
		 ply:EmitSound("npc/metropolice/pain"..math.random(1,8)..".wav") 


There is more of that, that’s why you don’t see the “end”

Turned into TUT: https://dl.dropboxusercontent.com/u/26074909/tutoring/sound/player_injury_sounds.lua.html

Alright, so what you’re trying to do is add a feature to a hook but you’re coding it like you’re replacing the function.

A few things…

GM/EntityTakeDamage is a GAMEMODE hook / function.

If you’re writing a game-mode, you’re ok to overwrite the function like so:


function GM:EntityTakeDamage( _t, _dmgInfo )

	-- stuff...

end

But, because it is supposed to enhance the functionality, you’ll want to use a hook ( runs before the main GM function example above, and prevents the main function from running if you return a NON NIL meaning false, true, Entity, etc… So, with hooks you only want to target specific behavior and avoid returns except if you need them. With this you won’t…

You may consider simplifying your logic too, I’ll show you how.

First, we make the hook.


hook.Add( "EntityTakeDamage", "InjurySounds", function( _t, _dmgInfo )

	-- stuff...

end )

Now, lets add some things to simplify the logic…


// We create a table of teams that this will affect; Lua is built around tables so using them with direct access is very fast; avoid table.HasValue especially in functions / hooks that run a LOT; this one runs meh amount depending on users and firefights.
local TEAMS_WHO_SCREAM = {
	[ TEAM_PC_RECRUE ] = true;
	[ TEAM_PC ] = true;
	[ TEAM_PC_MEDIC ] = true;
};

hook.Add( "EntityTakeDamage", "InjurySounds", function( _t, _dmgInfo )

	-- stuff...

end )

Now, lets set up our other requirements… Player and must be bullet damage and bring the table into it… I’m writing a little more airy than I typically do so you can see the individual parts better:


// We create a table of teams that this will affect; Lua is built around tables so using them with direct access is very fast; avoid table.HasValue especially in functions / hooks that run a LOT; this one runs meh amount depending on users and firefights.
local TEAMS_WHO_SCREAM = {
	[ TEAM_PC_RECRUE ] = true;
	[ TEAM_PC ] = true;
	[ TEAM_PC_MEDIC ] = true;
};

hook.Add( "EntityTakeDamage", "InjurySounds", function( _t, _dmgInfo )

	// Remember, with hooks we only return if we need to, and we focus on specific logic
	if ( IsValid( _t ) && _t:IsPlayer( ) ) then

		// I typically only declare variables when they're needed 2 or more times; here they aren't, but I'm just showing different ways to compartmentalize things to make them easier to understand / building blocks..

		// My naming technique for a boolean statement/return variable
		local _bBulletDamage = _dmgInfo:IsBulletDamage( );

		// Because the team is the key to the table and the values are true ( can be almost anything else too ) we can bypass having to run _t:Team( ) == ... || _t:Team( ) == ..., etc... we can call it once and be done with it...
		local _bOnScreamTeam = ( TEAMS_WHO_SCREAM[ _t:Team( ) ] );

		// If bullet damage, and they're a screamer, randomly yell out in pain...
		if ( _bBulletDamage && _bOnScreamTeam ) then
			 _t:EmitSound( "npc/metropolice/pain" .. math.random( 1, 8 ) .. ".wav" );
		end

	end

end );

A big thank to you ! You make me understand + it work ! You are the best !