• This is really annoying me now.
    6 replies, posted
So, I have a HUD for my Gamemode. When a player loads, they are given the error [CODE] [ERROR] gamemodes/unityroleplay/gamemode/gui/cl_hud.lua:88: attempt to call method 'Team' (a nil value) 1. v - gamemodes/unityroleplay/gamemode/gui/cl_hud.lua:88 2. unknown - lua/includes/modules/hook.lua:84 [/CODE] When I find the team, from the client, I get [CODE]] lua_run_cl print(LocalPlayer():Team()) 1 [/CODE] If I edit the lua file, even if it is adding a space to the end of the file, it will suddenly work. Unity I reconnect. Here is the code for line 87, 88 and 89 [CODE] draw.SimpleText(ply:GetNWString("rpname") or ply:Nick(), "_hud_title1", 25, y-125, Color(255,255,255,255), 0, 2) draw.SimpleText(JOBS[ply:Team()].name .. " - " .. GetPaycheck( ply ), "_hud_title2", 25, y-100, Color(255,255,255,255), 0, 2) draw.RoundedBox( 0, 25, y-80, 200, 30, Color(0,0,0,235) ) [/CODE] What is wrong?
LocalPlayer() isn't valid when lua initializes.
Add "if ( !IsValid( LocalPlayer() ) ) then return end" check before you draw the HUD.
You could also wrap your HUDPaint hook or panel creation inside of an InitPostEntity hook so you don't need the check above. The client (and therefore LocalPlayer()) is valid after InitPostEntity is called.
[QUOTE=Mista Tea;47160690]You could also wrap your HUDPaint hook or panel creation inside of an InitPostEntity hook so you don't need the check above. The client (and therefore LocalPlayer()) is valid after InitPostEntity is called.[/QUOTE] Plot Twist: [CODE] function HudIsLife() draw.SimpleText(ply:GetNWString("rpname") or ply:Nick(), "_hud_title1", 25, y-125, Color(255,255,255,255), 0, 2) -snip- end hook.Add("HUDPaint", "HudIsLove", HudIsLife) [/CODE] [editline]17th February 2015[/editline] [QUOTE=Robotboy655;47160677]Add "if ( !IsValid( LocalPlayer() ) ) then return end" check before you draw the HUD.[/QUOTE] Unfortunately, this dosn't work.
Since we don't have the full code we can only speculate. As said before, LocalPlayer( ) isn't valid the first few times HUDPaint is called; this is why you need an IsValid check. We also don't know if you set ply to LocalPlayer( )... Take a look at this: [url]https://dl.dropboxusercontent.com/u/26074909/tutoring/hud/proper_hud_creation.lua.html[/url]
[QUOTE=mindfulhacker;47160725]Plot Twist: [CODE] function HudIsLife() draw.SimpleText(ply:GetNWString("rpname") or ply:Nick(), "_hud_title1", 25, y-125, Color(255,255,255,255), 0, 2) -snip- end hook.Add("HUDPaint", "HudIsLove", HudIsLife) [/CODE] [editline]17th February 2015[/editline] Unfortunately, this dosn't work.[/QUOTE] Ply in that code is not defined, of course it will error.
Sorry, you need to Log In to post a reply to this thread.