Dear readers,
I need some help at scripting [lua] for anti-friendly fire for teams.
So i have 2 teams: Allies and Axis. I want that Allies can't kill there own team but can kill the Axis team.
And that Axis can't kill there own team but can kill the Allies team.
I used this script but it doesn't work:
local ANTI_TEAMKILL_TEAMS_TABLE = {
// Team 1
[ TEAM_ALLIES ] = 1;
// Team 2
[ TEAM_AXIS ] = 2;
Can someone help me?
Thank you :)
Regards,
E-Servers
Is that script you posted incomplete? It just looks like a table to me.
[editline]14th August 2017[/editline]
But anyway, you'd check the teams of the two players in a [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/GM/PlayerShouldTakeDamage]GM:PlayerShouldTakeDamage[/url] hook.
[QUOTE=code_gs;52571229]Is that script you posted incomplete? It just looks like a table to me.
[editline]14th August 2017[/editline]
But anyway, you'd check the teams of the two players in a [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/GM/PlayerShouldTakeDamage]GM:PlayerShouldTakeDamage[/url] hook.[/QUOTE]
Whole script:
local ANTI_TEAMKILL_TEAMS_TABLE = {
// Team 1
[ TEAM_ALLIES ] = 1;
// Team 2
[ TEAM_AXIS ] = 2;
//
// Perform the action that prevents team-damage between like-teams
//
hook.Add("PlayerShouldTakeDamage", "PlayerShouldTakeDamage:AvoidTeamDamage", function( _victim, _attacker )
// Make sure both victim and attacker are valid entities and that they're both player entities -- The victim can be assumed a player because of the hook. We're just calling it to remain uniform.
if ( IsValid( _victim ) && IsValid( _attacker ) && _victim:IsPlayer( ) && _attacker:IsPlayer( ) ) then
// If both victim and attacker team data matches, don't allow damage. This is the ONLY test we need to perform.
if ( ANTI_TEAMKILL_TEAMS_TABLE[ _victim:Team( ) ] == ANTI_TEAMKILL_TEAMS_TABLE[ _attacker:Team( ) ] || _victim:Team( ) == TEAM_CITIZEN ) then
return false;
end
end
end );
That code looks fine, minus the fact that you are missing a close bracket for your table, it's probably declared too early for the teams to be setup, and TEAM_CITIZEN is a bit of a dangling reference in your explanation. Also, you don't need the IsValid() checks, nor the _victim player check since that is the guarantee of the hook implementation.
In the future, please use [code] tags and post errors along with your code.
[QUOTE=code_gs;52571263]That code looks fine, minus the fact that you are missing a close bracket for your table, it's probably declared too early for the teams to be setup, and TEAM_CITIZEN is a bit of a dangling reference in your explanation. Also, you don't need the IsValid() checks, nor the _victim player check since that is the guarantee of the hook implementation.
In the future, please use [code] tags and post errors along with your code.[/QUOTE]
So what would the code be? sorry im new.
[code]
local ANTI_TEAMKILL_TEAMS_TABLE = {
// Team 1
[ TEAM_ALLIES ] = 1;
// Team 2
[ TEAM_AXIS ] = 2;}
hook.Add("PlayerShouldTakeDamage", "PlayerShouldTakeDamage:AvoidTeamDamage", function( _victim, _attacker )
if ( ANTI_TEAMKILL_TEAMS_TABLE[ _victim:Team( ) ] == ANTI_TEAMKILL_TEAMS_TABLE[ _attacker:Team( ) ] || _victim:Team( ) == TEAM_CITIZEN ) then
return false;
end
end );
[/code]
Knowing me I have probably screwed up something. I'm sure you can fix it if I have done something wrong.
This should work perfectly.
[code]
hook.Add("PlayerShouldTakeDamage", "AntiTeamKill", function( victim, attacker )
if attacker:IsPlayer() and ( victim:Team() == attacker:Team() ) then -- Check if the attacker and victim is the same team
if ( victim:Team() == TEAM_AXIS or victim:Team() == TEAM_ALLIES ) then -- Check if the victim is one of the teams.
return false --- If they are, disable the damage.
end
end
end )
[/Code]
Sorry, you need to Log In to post a reply to this thread.