Broken Lua Code

I know this is probably just an error on my end, but I’m trying. I slap this code down at the bottom of my jobs (I’m pretty sure that’s not where it goes) and it doesn’t work. There aren’t any lua errors but the jobs still don’t have any extra health…


hook.Add("PlayerSpawn", "hp", function(ply)
if ( ply:Team() == "Admin on Duty" ) then
    ply:SetHealth( 10000 );
elseif ( ply:Team() == "Moderator on Duty" ) then
    ply:SetHealth( 10000 );
else
     ply:SetArmor( 0 );
end
end)

The team isn’t going to be the name of the job, it’ll be TEAM_ADMIN

Yeah, I left out some info. I tried TEAM_ADMIN and then I did the “Admin on Duty” but neither of them work.

The ply:Team() method just returns the team number if I recall correctly. In the case of TEAM_ADMIN, this would just be a variable of an int type:


TEAM_ADMIN = 1

Check where your teams are set up. The wiki has this example:



team.SetUp( 2, "Mingebags", Color( 255, 0, 0 ) )


This could easily become:



TEAM_MINGEBAGS = 10
team.SetUp( TEAM_MINGEBAGS, "Mingebags", Color( 255, 0, 0 ) )


So to check that someone is on the “Mingebags” team, you could do:



if( ply:Team() == TEAM_MINGEBAGS ) then ... end


Or



if( ply:Team() == 20 ) then ... end


Both would have the same effect.

Would I have to still do the function?


function(ply)
if( ply:Team() == TEAM_MINGEBAGS ) then ply:SetHealth (10000) end

Yes you would need all of that:



hook.Add("PlayerSpawn", "hp", function(ply)
if ( ply:Team() == "Admin on Duty" ) then
    ply:SetHealth( 10000 );
elseif ( ply:Team() == "Moderator on Duty" ) then
    ply:SetHealth( 10000 );
else
     ply:SetArmor( 0 );
end
end)


If we separate out what’s actually going on, we have:



hook.Add( string "HookReference", string "UniqueKey", <your function here> )


can be a function reference like below:



hook.Add( "PlayerSpawn", "MyPlayerSpawnHook", DoPlayerSpawn )
function DoPlayerSpawn( ply )
    ply:ChatPrint( "You've spawned!" )
end


Or can be what’s called an anonymous function (or enclosure) which is a function contained within the argument for hook.Add():



hook.Add( "PlayerSpawn", "MyPlayerSpawnHook", 
    function (ply)
        ply:ChatPrint( "You've spawned!" )
    end)


All the code above is untested and I haven’t written GMod Lua for over 3 years, so mistakes might be present.

Still doesn’t work and there’s no lua errors but thanks for the help, at least I learned some stuff. (: