Friendly Fire Script By Acecool Doesnt Work

So this Doesnt Seem To work at all Everyone can still hurt eachother

Code:




local ANTI_TEAMKILL_TEAMS_TABLE = {
    // Team 1
 [ TEAM_POLICE ] = 1;
 [ TEAM_POLICEGOLD ] = 1;
 [ TEAM_SWAT ]= 1;
    
// Team 2
[ TEAM_TERROR ]    = 2;
[ TEAM_TERRORLEAD ]    = 2;
--[ TEAM_SIX ]     = 2;
};
hook.Add("PlayerShouldTakeDamage", "PlayerShouldTakeDamage:AvoidTeamDamage", function( _victim, _attacker )
    if ( IsValid( _victim ) && IsValid( _attacker ) && _victim:IsPlayer( ) && _attacker:IsPlayer( ) ) then 
        if ( ANTI_TEAMKILL_TEAMS_TABLE[ _victim:Team( ) ] == ANTI_TEAMKILL_TEAMS_TABLE[ _attacker:Team( ) ] ) then
            return false;
        end
    end
end );



This is for DarkRp. Right? Also do you get any errors?

The script looks fine. Did you adjust your team names properly? Where did you put the script?

This script must be stolen from his upcoming gamemode Crime City RP!

I do not see the usual



// Le Autistic Script Name 
// By Josh 'Acefool' Moser


The FBI will be alerted about this!

It won’t work if this script runs before the teams are defined.

Chuck the whole thing in an Initialize hook.

Well i made a addon in the addons folder, and the directory for the script is Addons/Friendfire/lua/autorun/server/teamkill.lua

[editline]6th July 2014[/editline]

A player won’t be taking damage before teams are initialized.

No, but the TEAM_* variables would be nil, therefore the configuration table would fail to load.
That should throw up a Lua error, though, since nil indexes aren’t allowed.

He could just put them all as strings then, and do tostring( ply:Team() )

So The Script Is Faulty?

No, just has a team initialization issue.

So how could I Fix This

Here ya go:

[LUA]
hook.Add( “Initialize”, “noteamkillpls”, function()

--if the value for two teams is the same, then they won't be able to damage each other.
local teams = {
	[ TEAM_POLICE ] = 1,
	[ TEAM_POLICEGOLD ] = 1,
	[ TEAM_SWAT ]= 1,

	[ TEAM_TERROR ] = 2,
	[ TEAM_TERRORLEAD ] = 2,
}

hook.Add("PlayerShouldTakeDamage", "PlayerShouldTakeDamage:AvoidTeamDamage", function( victim, attacker )

	if IsValid( attacker ) and attacker:IsPlayer() then 

		if teams[victim:Team()] == teams[attacker:Team()] then

			return false

		end

	end

end )

end )
[/LUA]

hopefully it should be much more readable than the previous code you had.
edit: Oops meant to use Initialize hook. Fixed.
edit2: lol @ matt and anon dumbing everything in this thread

Only when you used the InitPostEntity hook instead of Initialize.