Learning Lua, making a game mode as a test, need HUD help

I’m not quite sure how to do a custom properly in my custom game mode. So far I have it hiding the original HUD and drawing a rectangle on the hud, but I can’t get it to draw my HP properly. When joining the server you start as a spectator and the HUD script was giving an error about the player being a null entity so I changed the code to accommodate that but now the HP stays at 0.
[lua]local ply = LocalPlayer()
if ply != NULL then
hp = ply:Health()
else
hp = 0
end
function chud ()
draw.RoundedBox(5,15,ScrH()-175,250,160,Color(0,0,0,150))
surface.CreateFont (“coolvetica”,20,400,true,false,“hudFont”)
draw.SimpleTextOutlined(“Tier: “…ply.Gun…”/6”,“hudFont”,25,ScrH()-35,Color(255,255,255,255),TEXT_ALIGN_LEFT,TEXT_ALIGN_LEFT,2,Color(0,0,0,255))
draw.SimpleTextOutlined("HP: "…hp,“hudFont”,25,ScrH()-35,Color(255,255,255,255),TEXT_ALIGN_LEFT,TEXT_ALIGN_LEFT,2,Color(0,0,0,255))
end
hook.Add(“HUDPaint”,“hud”,chud)
function hidehud(name)
for k, v in pairs{“CHudHealth”, “CHudBattery”, “CHudAmmo”, “CHudSecondaryAmmo”} do
if name == v then return false end
end
end
hook.Add(“HUDShouldDraw”, “hidehud”, hidehud)
[/lua]

You need to update the health every frame by calling player.Health in the HUDPaint hook. Instead of setting hp before the hook, just replace hp in the hook with LocalPlayer():Health() (you shouldn’t set ply outside the hook either, because it will always be NULL when the code is run).

Thanks, that fixed the HP. The score system I made however still doesn’t update. I changed all the ply to LocalPlayer().
It looks like this now:
[lua]function HUDPaint()
local ply = LocalPlayer()
if ply != NULL then
hp = LocalPlayer():Health()
local gunrank = LocalPlayer().Gun
else
hp = 0
local gunrank = 0
end
if gunrank == nil then
gunrank=0
end
draw.RoundedBox(5,15,ScrH()-175,250,160,Color(0,0,0,150))
surface.CreateFont (“coolvetica”,20,400,true,false,“hudFont”)
draw.SimpleTextOutlined("Tier : "…gunrank,“hudFont”,25,ScrH()-75,Color(255,255,255,255),TEXT_ALIGN_LEFT,TEXT_ALIGN_LEFT,2,Color(0,0,0,255))
draw.SimpleTextOutlined("HP : "…hp,“hudFont”,25,ScrH()-35,Color(255,255,255,255),TEXT_ALIGN_LEFT,TEXT_ALIGN_LEFT,2,Color(0,0,0,255))
end
hook.Add(“HUDPaint”,“hud”,HUDPaint)
function hidehud(name)
for k, v in pairs{“CHudHealth”, “CHudBattery”, “CHudAmmo”, “CHudSecondaryAmmo”} do
if name == v then return false
end
end
end
hook.Add(“HUDShouldDraw”, “hidehud”, hidehud)[/lua]

[editline]18th January 2011[/editline]

It turns out gunrank is coming up as nill. Any idea on how to fix it? It looks like it should work to me.

Your gunrank variable is local to the if statement and goes out of scope by the time you come to use it.

[lua]function HUDPaint()
local hp = LocalPlayer():Health() – LocalPlayer will not be NULL in HUDPaint
local gunrank = LocalPlayer().Gun
draw.RoundedBox(5, 15, ScrH()-175, 250, 160, Color(0, 0, 0, 150))
surface.CreateFont(“coolvetica”, 20, 400, true, false, “hudFont”)
draw.SimpleTextOutlined("Tier : "…gunrank, “hudFont”, 25, ScrH()-75, Color(255, 255, 255, 255), TEXT_ALIGN_LEFT, TEXT_ALIGN_LEFT, 2, Color(0, 0, 0, 255))
draw.SimpleTextOutlined("HP : "…hp, “hudFont”, 25, ScrH()-35, Color(255, 255, 255, 255), TEXT_ALIGN_LEFT, TEXT_ALIGN_LEFT, 2, Color(0, 0, 0, 255))
end
hook.Add(“HUDPaint”, “hud”, HUDPaint)

local function hidehud(name)
for k, v in pairs{“CHudHealth”, “CHudBattery”, “CHudAmmo”, “CHudSecondaryAmmo”} do
if name == v then return false end
end
end
hook.Add(“HUDShouldDraw”, “hidehud”, hidehud)[/lua]

I found the problem, the Gun variable is on the server side, not the client side. How can I tell the client what it is?

[editline]18th January 2011[/editline]

Also how can I get the name of a swep? Like 9mm Pistol from weapon_pistol. Also the kill icon for it.

Name of swep is
[lua]
ply:GetActiveWeapon():GetClass()
[/lua]

From there I think the only way to change it from weapon_<something> would be if statements or using string manipulation to remove the ‘weapon_’

Thank you what about the kill icon?

Honestly not sure, I can’t see a general function on the wiki to get the kill icon, I’ll search around.

E:
Might be possible to try using CHudDeathNotice but its a long shot and its not documented on the wiki, sorry I can’t help more.

Couldn’t work anything out with that. Still need help with sending data from the server to client. Thanks for the other help though, works perfectly.

You need to use user messages, this might work for ya.

[lua]

–This goes in a client file

function getGunInfo( data )

LocalPlayer().Gun = data:ReadLong() -- or data:ReadString() if it's a string.

end
usermessage.Hook(“sendGunInfo”, getGunInfo)

[/lua]

and now:

[lua]

– this will go in whatever fuction on the server you update the ply.Gun

umsg.Start( “sendGunInfo”, ply)
umsg.Long( ply.Gun) – or umsg.String( ply.Gun)
umsg.End()

[/lua]

It’s good to note that anything you send in a usermessage is read out in the hook in the same order.

Dear lord what is with OP’s tabbing.

Actually never mind, the error shows up after reloading the game mode after a previous error

[editline]21st January 2011[/editline]

How do I stop the player from spawning when they join the server?

For the name of the weapon i think you can use http://wiki.garrysmod.com/?title=Weapon.GetPrintName

Yeah, already found that one. In my code on the server side,
[lua]if killer:GetActiveWeapon():GetClass() == “weapon_crowbar” then[/lua]
GetActiveWeapon is giving me nil when the weapon is a crossbow or RPG.

you need a _ instead of a space between weapon and crowbar.

scroll down on the lau highlighter

problem solved, I was getting the entity that killed the person and not the inflictor.