Anti Team Kill For Darkrp Support

Hi Facepunch !

I’ ve been working on a "Anti team kill " addon for darkrp. I’ m coding a server and i need this addon to prevent troll to team kill person witch are in their own team …
This is how my job are working :

*SWAT
SWAT Soldier
SWAT Caporal
SWAT Capitain
SWAT Commender

TERRO
TERRO Soldier
TERRO Caporal
TERRO Capitain
TERRO Commender
*
This is how i want it to work : SWAT can kill TERRO , TERRO can kill SWAT but SWAT cant kill SWAT and TERRO cant kill TERRO


This is what i’ ve done/found :

Anti team kill for TTT ( This will prevent detectives from killing detectives and traitors from killing traitors. )


if SERVER then

	--[[Configs
	true = Enable and false = Disable
	Changing any of these configs will take
	direct effect without needing to restart your server.]]
	
	-- Traitor Deathmatch
	local TraitorAttackerNotify = true
	local TraitorVictimNotify = true
	local TraitorServerLog = true
	local TraitorAntiRDM = true -- This needs to be set to true for the traitor configs to work.
	
	-- Detective Deathmatch
	local DetectiveAttackerNotify = true
	local DetectiveVictimNotify = true
	local DetectiveServerLog = true
	local DetectiveAntiRDM = true -- This needs to be set to true for the detective configs to work.
	
	-- Anti Deathmatch Function
	local function antiteamkill(victim, attacker)
		
		if victim == attacker or attacker == victim then return end
		
		if victim:IsPlayer() and attacker:IsPlayer() and IsValid(victim) and IsValid(attacker) then
		
			-- Anti Traitor Deathmatch
			if victim:GetRole() == ROLE_TRAITOR and attacker:GetRole() == ROLE_TRAITOR then
				
				if TraitorAntiRDM == true and TraitorAttackerNotify == true then
					attacker:ChatPrint("You were attempting to harm your traitor buddy, " ..victim:Nick().. ".")
				end
					
				if TraitorAntiRDM == true and TraitorVictimNotify == true then
					victim:ChatPrint("Your traitor buddy, " ..attacker:Nick().. " was attempting to harm you.")
				end
					
				if TraitorAntiRDM == true and TraitorServerLog == true then
					DamageLog("TKA:      " ..attacker:Nick().. " [traitor] was attempting to harm " ..victim:Nick().. " [traitor]")
				end
					
				if TraitorAntiRDM == true then
					return false
				else
					return true
				end
				
			end
			
			-- Anti Detective Deathmatch
			if victim:GetRole() == ROLE_DETECTIVE and attacker:GetRole() == ROLE_DETECTIVE then
				
				if DetectiveAntiRDM == true and DetectiveAttackerNotify == true then
					attacker:ChatPrint("You were attempting to harm your detective buddy, " ..victim:Nick().. ".")
				end
					
				if DetectiveAntiRDM == true and DetectiveVictimNotify == true then
					victim:ChatPrint("Your detective buddy, " ..attacker:Nick().. " was attempting to harm you.")
				end
					
				if DetectiveAntiRDM == true and DetectiveServerLog == true then
					DamageLog("TKA:      " ..attacker:Nick().. " [detective] was attempting to harm " ..victim:Nick().. " [detective]")
				end
					
				if DetectiveAntiRDM == true then
					return false
				else
					return true
				end
				
			end
			
		end
		
	end
	hook.Add("PlayerShouldTakeDamage", "antiteamkill", antiteamkill)
	
end

I need this for darkrp . I’ ve already make some basic code but i lost it due to unsave work … ( My work was not even working )
This is why i’ ll realy apriciate if some of you guys could help me with this !

**If you help me with this I don’ t see any problem to upload it on the website , just for you to be able to use it :wink: **

Thanks you !

[lua]hook.Add(“PlayerShouldTakeDamage”,“NoTeamkillPls”,function(ply,att,)
if ply:Team()==att:Team() then return false end
end)[/lua]
You get the basic idea, Get the team the player is on and just check to see if they are part of the SWAT or part of the other faction you listed, then check if the attacker and victim are on the same team, if they are then return false

Take a look at my system: https://dl.dropboxusercontent.com/u/26074909/tutoring/_systems/simple_anti_teamkill_system/anti_teamkill_system_with_failsafe.lua.html - Remove .html to view .lua

It allows you to assign a common ID over multiple “teams” and use the id to distinguish between teams instead of the actual team key.

Just set the table up and the first time someone gets damaged it’ll build the table ( had to do it this way because if it is installed as an addon, GM won’t exist and CreateTeams may not have ran all the way if I used that hook ) and it’ll prevent damage for people with the same id…

This will allow unlimited team ids and unlimited teams to be added to the table. so the table would look something like:


	// Set the teams up on the first occasion when damage is dealth...
	ANTI_TEAMKILL_TEAMS_TABLE = {
		// Team 1
		[ TEAM_SWAT ] 	= 1;
		[ TEAM_SWAT_SOLDIER ] 	= 1;
		[ TEAM_SWAT_CORPORAL ] 	= 1;
		[ TEAM_SWAT_CAPTAIN ] 	= 1;
		[ TEAM_SWAT_COMMANDER ] 	= 1;

		// Team 2
		[ TEAM_TERROR ] 	= 2;
		[ TEAM_TERROR_SOLDIER ] 	= 2;
		[ TEAM_TERROR_CORPORAL ] 	= 2;
		[ TEAM_TERROR_CAPTAIN ] 	= 2;
		[ TEAM_TERROR_COMMANDER ] 	= 2;
	};
	

Thanks you guys for your help i’ ll take a look at this today !

Ok so this is what i’ m using :


//
// Stop Team Damage with failsafe ( auto-refresh compatible ) - Josh 'Acecool' Moser
//


//
// Configuration Table - Make sure the teams you want immunity between share the same number
//
ANTI_TEAMKILL_TEAMS_TABLE = ANTI_TEAMKILL_TEAMS_TABLE || { };


//
// Changed to a function which gets called on damage instead of attempting to figure out when
// DarkRP sets up the teams...
//
local function AntiTeamKillBuildTable( )
	// Set the teams up on the first occasion when damage is dealth...
	ANTI_TEAMKILL_TEAMS_TABLE = {
		// Team 1
		[ TEAM_AMERICAIN ] 	= 1;
		[ TEAM_SWATING ] 	= 1;
		[ TEAM_ACAPORAL ] 	= 1;

		// Team 2
		[ TEAM_REBELVIP] 	= 2;
		[ TEAM_REBEL ] 	= 2;
		[ TEAM_RCAPORAL ] 	= 2;
	};

	// Insert any left-over teams as rogues...
	for k, v in pairs( team.GetAllTeams( ) ) do
		// Only insert if the entry doesn't exist; this allows you to set up 1 team, and everyone else gets their own entry...
		if ( ANTI_TEAMKILL_TEAMS_TABLE[ k ] ) then CONTINUE; end

		// Set it up, give it a team number which is offset by a bit...
		ANTI_TEAMKILL_TEAMS_TABLE[ k ] = k + 9000;
	end
end


//
// When making hooks, it is best to remain non-intrusive. Other hooks may be running to determine damage in addition to this one.
// To remain non-intrusive we create the scenario which matches the logic we're using; nothing else should be touched.
//

//
// 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
		// ANTI_TEAMKILL_TEAMS_TABLE is defined as an empty table, at least, so here we query the teams...
		local _teamA = ANTI_TEAMKILL_TEAMS_TABLE[ _attacker:Team( ) ];
		local _teamV = ANTI_TEAMKILL_TEAMS_TABLE[ _victim:Team( ) ];

		// Set up the table when it needs to be set up ( first damage occasion instead of guessing with DarkRP / other gamemodes )...
		if ( !ANTI_TEAMKILL_TEAMS_TABLE || !_teamA || !_teamV ) then
			// Build the table
			AntiTeamKillBuildTable( );

			// Reset the values
			_teamA = ANTI_TEAMKILL_TEAMS_TABLE[ _attacker:Team(1) ];
			_teamV = ANTI_TEAMKILL_TEAMS_TABLE[ _victim:Team(2) ];
		end

		// If both victim and attacker team data matches, don't allow damage. This is the ONLY return we need to handle.
		if ( ANTI_TEAMKILL_TEAMS_TABLE[ _victim:Team(1) ] == ANTI_TEAMKILL_TEAMS_TABLE[ _attacker:Team(2) ] ) then
			return false;
		end
	end
end );

The team can still kill each over … I keep trying to find a way out !

[editline]12th February 2015[/editline]

Seems that you forget and ‘=’ near the ‘;’ , here is the ERROR :

[ERROR] addons/atk/lua/autorun/server/antiteamkill.lua:33: ‘=’ expected near ‘;’

[editline]12th February 2015[/editline]


I’ ve found the way to do this and now the script is done , thanks you for your help i will upload it soon on the site - This will prevent people witch are in the same team to kill each other :

Exemple
[ TEAM_REBEL_VIP]
[ TEAM_REBEL ]
[ TEAM_REBEL_CAPORAL ]
[ TEAM_REBEL_LIEUTANT ]
[ TEAM_REBEL_CAPITAIN ]
[ TEAM_REBEL_MAJOR]
[ TEAM_REBEL_COMMANDANT ]
[ TEAM_REBEL_MARECHAL ]
||
|| Can kill
v______________________________________________________
^
|| Can kill
||
[ TEAM_AMERICAIN ]
[ TEAM_SWATING ]
[ TEAM_AMERCICAN_CAPORAL ]
[ TEAM_AMERCICAN_RTILLEURA ]
[ TEAM_AMERCICAN_LIEUTENANT ]
[ TEAM_AMERCICAN_CAPITAIN ]
[ TEAM_AMERCICAN_MAJOR ]
[ TEAM_AMERCICAN_COMMANDANT ]
[ TEAM_AMERCICAN_MARECHAL ]

But the american can not kill each other and same thing for the rebel …

Thanks you for helping me out !


Yep, you need to associate all teams on a side with a single id to prevent team killing.

can i please have the fixed version of this ???


(User was banned for this post ("Dumb bumps" - Novangel))