Help with hook.Add(PlayerSpawn

So I am having issues with my player getting their health set when they spawn.


hook.Add("PlayerSpawn","LoadHealth", function(ply)  local lvl = ply:GetNWInt("LevelNum")-1     ply:SetHealth(200 + lvl * 5) end)  

Its supposed the health on spawn. By Base health being 100 but adding the players cuttent level * 5 to the health. But it is not working.

[editline]29th May 2013[/editline]

So guys I changed the hook.Add to PlayerDeath instead of PlayerSpawn. And upon death it ran my function that’s hooked. So why is it glitching when I run it with player spawn? Is there a alternative? I tried printing to see if there were errors but there are none. Help please, this is sincerely starting to aggravate me. Garry added many great things with GMOD13 Update. But he also challenged us new comers more because the experience level for us still isn’t there.

[lua]hook.Add(“PlayerSpawn”, “LoadHealth_check”, function(ply)
print(“A player has spawned”)
end)[/lua]

If that doesn’t print ‘A player has spawned’ when someone spawns then you probably have another PlayerSpawn hook that is returning true or false and overriding any other hooks.
If it does print, try adding in your health setting code, and make sure it still prints.

It prints still with and without my code. It pretty much skips my code and prints that to console.


hook.Add("PlayerSpawn", "LoadHealth_check", function(ply)
           local lvl = ply:GetNWInt("LevelNum")-1
           ply:SetMaxHealth(100 + lvl * 5)
        ply:SetHealth(100 + lvl * 5)
    print("A player has spawned")
end)

I get the feeling that ply:GetNWInt(“LevelNum”) isn’t returning the value you expect, try this.

[lua]hook.Add(“PlayerSpawn”, “LoadHealth_check”, function(ply)
print(“A player is spawning”)
local lvl = ply:GetNWInt(“LevelNum”)
print(“Lvl is”,lvl)
lvl = lvl-1
print(“Fixed lvl is”,lvl)
print(“Setting max health to”, 100 + lvl*5)
ply:SetMaxHealth(100 + lvl * 5)
ply:SetHealth( ply:GetMaxHealth() )
end)[/lua]

I’m dumbfounded. Like I really am stooped on this. I replaced my code with that code you put. And it printed out all the prints in console, but totally skipped all the other code, yet no errors either. WTF? Lol…

Try printing health after all of it, maybe it’s changing but you don’t notice it.
@below - Try anyways

It’s not because it would update the health upon spawn.

One final idea, there may be other spawn hooks that are running after this code and changing the health back to normal. If running this in a timer works then I think that is what is happening.

[lua]hook.Add(“PlayerSpawn”, “LoadHealth_check”, function(ply)
timer.Simple(0.1, function()
print(“A player is spawning”)
local lvl = ply:GetNWInt(“LevelNum”)
print(“Lvl is”,lvl)
lvl = lvl-1
print(“Fixed lvl is”,lvl)
print(“Setting max health to”, 100 + lvl*5)
ply:SetMaxHealth(100 + lvl * 5)
print(“Setting player health to”, ply:GetMaxHealth() )
ply:SetHealth( ply:GetMaxHealth() )
end)
end)[/lua]

I changed it to a function alone instead of a hook and it ran perfectly. I used GM:PlayerSpawn fucntion. I appreciated the help though!