Please help with script!

[lua]
OnPlayerChangedTeam = function ( ply, oldTeam, newTeam )
if(ply:Team() == TEAM_ADMIN ) then
ply:GodEnable()
elseif( ply:Team() ~= TEAM_ADMIN )
ply:GodDisable()
end
end

hook.Add( “OnPlayerChangedTeam”, “LOLFAG”, OnPlayerChangedTeam)
[/lua]

I need help!

Perhaps you could tell us what the hell is not working?

You’re missing a then on the line with elseif… Also, the elseif isn’t needed, just an else will do because its the opposite logic you’re testing for which is what an else does.

I also thing you should combine the function into the hook…

[lua]hook.Add( “OnPlayerChangedTeam”, “UniqueName”, function( ply, oldTeam, newTeam )
if ( ply:Team( ) == TEAM_ADMIN ) then
ply:GodEnable( );
else // if ( ply:Team( ) != TEAM_ADMIN ) then – The elseif isn’t needed; else means exactly what the elseif is trying to accomplish here
ply:GodDisable( );
end
end ); – When writing it so the function is integrated with the hook, the end for the function needs a ) after it to end the hook.Add call[/lua]

It would make error

what is the fucking error? we cant be any clearer

Use what I just pasted; the error is THEN expected after ‘)’ or something like that.

Just remove the elseif because it is redundant, and use an else…

Here, 2 versions which work assuming you have the functions setup:

[lua]hook.Add( “OnPlayerChangedTeam”, “UniqueName”, function( ply, oldTeam, newTeam )
if ( ply:Team( ) == TEAM_ADMIN ) then
ply:GodEnable( );
elseif ( ply:Team( ) != TEAM_ADMIN ) then
ply:GodDisable( );
end
end );[/lua]

[lua]hook.Add( “OnPlayerChangedTeam”, “UniqueName”, function( ply, oldTeam, newTeam )
if ( ply:Team( ) == TEAM_ADMIN ) then
ply:GodEnable( );
else
ply:GodDisable( );
end
end );[/lua]

In case you don’t have GodEnable/Disable set up:

SERVER-SIDE:
[lua]//
// Vars
//
local META_PLAYER = FindMetaTable( “Player” );

//
// Enable Godmode by setting a variable
//
function META_PLAYER:GodEnable( )
self.__GodMode = true;
end

//
// Disable Godmode by setting a variable
//
function META_PLAYER:GodDisable( )
self.__GodMode = false;
end

//
// Make sure players in godmode don’t take damage
//
hook.Add( “PlayerShouldTakeDamage”, “GodModeSystem”, function( _victim, _attacker )
if ( _victim.__GodMode ) then
return false;
end
end );[/lua]

What’s wrong with GMod’s god system?

I didn’t know whether or not those functions existed; however I believe the default system resets if you die, or sometimes it allows you to get damaged and if you die from that then godmode is disabled, but you have to toggle it twice to get it to work again, or something.

This will work as is regardless of whether or not suicide occurred.

Might be worth experimenting with ply:AddFlags(FL_GODMODE) and ply:RemoveFlags(FL_GODMODE). GMod’s ply:GodEnable() and ply:GodDisable() are obsolete now IIRC. The plus side to the flags system is that you can check if a flag is set using ply:IsFlagSet(FL_*). AddFlags and RemoveFlags is also shared though I don’t know whether this is required or not for godmode.

thanks!