Spectator HUD Help

Im trying to get my HUD to show the player you’re spectating’s health, name etc but it comes up with this error:

[ERROR] gamemodes/uppercut/gamemode/cl_hud.lua:20: attempt to index local ‘tgt’ (a nil value)

  1. SpecHUDPaint - gamemodes/uppercut/gamemode/cl_hud.lua:20
  2. unknown - gamemodes/uppercut/gamemode/cl_hud.lua:329

This is my code:


function SpecHUDPaint()
	local client = LocalPlayer()
	local tgt = client:GetObserverTarget()
	local health = client:Health()
	local clhealth = tgt:Health()
	local kd = math.Round(100 * client:Frags() / client:Deaths()) / 100
	local clkd = math.Round(100 * tgt:Frags() / tgt:Deaths()) / 100
	local name = client:Nick()
	local clname = tgt:Nick()
	local GetLang = LANG.GetUnsafeLanguageTable
	local karma = client:GetBaseKarma()
	local clkarma = tgt:GetBaseKarma()
	local L = GetLang()
	local roundstate_string = {
   [ROUND_WAIT]   = "round_wait",
   [ROUND_PREP]   = "round_prep",
   [ROUND_ACTIVE] = "round_active",
   [ROUND_POST]   = "round_post"
	};
	
//TIME
	draw.RoundedBox( 8, ScrW() - (ScrW() - 30), ScrH() - (ScrH() - 35), 110, 50, Color( 125, 125, 125, 125 ) )
	draw.RoundedBox( 8, ScrW() - (ScrW() - 20), ScrH() - (ScrH() - 25), 130, 70, Color( 0, 0, 0, 200 ) )
	draw.SimpleText(os.date( "%a, %I:%M:%S %p" ), "CenterPrintText", 38, 60, hudtable.colorwhite)
	draw.SimpleText(os.date( "%m/%d/20%y" ), "CenterPrintText", ScrW() - (ScrW() - 40), ScrH() - (ScrH() - 40), hudtable.colorwhite)
	
//NAME
if IsValid(tgt) and tgt:IsPlayer() then
	draw.SimpleText(clname, "ChatFont", ScrW() - (ScrW() - 40), ScrH() - 150, hudtable.colorwhite )
else
	draw.SimpleText(name, "ChatFont", ScrW() - (ScrW() - 40), ScrH() - 150, hudtable.colorwhite )
end
 
//group name
if IsValid(tgt) and tgt:IsPlayer() then
	if
		tgt:IsUserGroup("owner") then draw.SimpleText("Owner", "ChatFont", ScrW() - (ScrW() - 145), ScrH() - 125, hudtable.colorwhite )
	elseif
		tgt:IsUserGroup("superadmin") then draw.SimpleText("SuperAdmin", "ChatFont", ScrW() - (ScrW() - 120), ScrH() - 125, hudtable.colorwhite )
	elseif 
		tgt:IsUserGroup("admin") then draw.SimpleText("Admin", "ChatFont", ScrW() - (ScrW() - 145), ScrH() - 125, hudtable.colorwhite )
	elseif
		tgt:IsUserGroup("moderator") then draw.SimpleText("Moderator", "ChatFont", ScrW() - (ScrW() - 145), ScrH() - 125, hudtable.colorwhite )
	elseif 
		tgt:IsUserGroup("vip") then draw.SimpleText("VIP", "ChatFont", ScrW() - (ScrW() - 145), ScrH() - 125, hudtable.colorwhite ) 
	elseif
		tgt:IsUserGroup("regular") then draw.SimpleText("Regular", "ChatFont", ScrW() - (ScrW() - 145), ScrH() - 125, hudtable.colorwhite )    
	elseif 
		tgt:IsUserGroup("user") then draw.SimpleText("User", "ChatFont", ScrW() - (ScrW() - 145), ScrH() - 125, hudtable.colorwhite )
	else
		if
			client:IsUserGroup("owner") then draw.SimpleText("Owner", "ChatFont", ScrW() - (ScrW() - 145), ScrH() - 125, hudtable.colorwhite )
		elseif
			client:IsUserGroup("superadmin") then draw.SimpleText("SuperAdmin", "ChatFont", ScrW() - (ScrW() - 120), ScrH() - 125, hudtable.colorwhite )
		elseif 
			client:IsUserGroup("admin") then draw.SimpleText("Admin", "ChatFont", ScrW() - (ScrW() - 145), ScrH() - 125, hudtable.colorwhite )
		elseif
			client:IsUserGroup("moderator") then draw.SimpleText("Moderator", "ChatFont", ScrW() - (ScrW() - 145), ScrH() - 125, hudtable.colorwhite )
		elseif 
			client:IsUserGroup("vip") then draw.SimpleText("VIP", "ChatFont", ScrW() - (ScrW() - 145), ScrH() - 125, hudtable.colorwhite ) 
		elseif
			client:IsUserGroup("regular") then draw.SimpleText("Regular", "ChatFont", ScrW() - (ScrW() - 145), ScrH() - 125, hudtable.colorwhite )    
		elseif 
			client:IsUserGroup("user") then draw.SimpleText("User", "ChatFont", ScrW() - (ScrW() - 145), ScrH() - 125, hudtable.colorwhite )
		end
	end
end
	
//Kill Death Ratio
if IsValid(tgt) and tgt:IsPlayer() then
	if client:Deaths() > 0 then
		draw.SimpleText("K/D = "..clkd, "ChatFont", ScrW() - (ScrW() - 40), ScrH() - 125, hudtable.colorwhite )
	else draw.SimpleText("K/D = 0", "ChatFont", ScrW() - (ScrW() - 40), ScrH() - 125, hudtable.colorwhite ) end
else
	if client:Deaths() > 0 then
		draw.SimpleText("K/D = "..kd, "ChatFont", ScrW() - (ScrW() - 40), ScrH() - 125, hudtable.colorwhite )
	else draw.SimpleText("K/D = 0", "ChatFont", ScrW() - (ScrW() - 40), ScrH() - 125, hudtable.colorwhite ) end
end
 
//HEALTH
if IsValid(tgt) and tgt:IsPlayer() then
	draw.RoundedBox(4, ScrW() - (ScrW() - 40), ScrH() - 100, 300 , 30, Color(0,0,0,255))
	draw.RoundedBox(4, ScrW() - (ScrW() - 40), ScrH() - 100, health * 3 , 30, Color(200,0,0,255))
	draw.RoundedBox(4, ScrW() - (ScrW() - 40), ScrH() - 100, health * 3 , 20, Color(255,0,0,255))
	draw.SimpleText(health, "Trebuchet24", ScrW() - (ScrW() - 165), ScrH() - 98, hudtable.colorwhite )	
else
	draw.RoundedBox(4, ScrW() - (ScrW() - 40), ScrH() - 100, 300 , 30, Color(0,0,0,255))
	draw.RoundedBox(4, ScrW() - (ScrW() - 40), ScrH() - 100, health * 3 , 30, Color(200,0,0,255))
	draw.RoundedBox(4, ScrW() - (ScrW() - 40), ScrH() - 100, health * 3 , 20, Color(255,0,0,255))
	draw.SimpleText(clhealth, "Trebuchet24", ScrW() - (ScrW() - 165), ScrH() - 98, hudtable.colorwhite )
end
		
//KARMA
if IsValid(tgt) and tgt:IsPlayer() then
	draw.SimpleText(clkarma, "ChatFont", ScrW() - (ScrW() - 300), ScrH() - 125, hudtable.colorwhite )
	draw.SimpleText("Karma = ", "ChatFont", ScrW() - (ScrW() - 230), ScrH() - 125, hudtable.colorwhite )
	math.Round(karma)
else
	draw.SimpleText(karma, "ChatFont", ScrW() - (ScrW() - 300), ScrH() - 125, hudtable.colorwhite )
	draw.SimpleText("Karma = ", "ChatFont", ScrW() - (ScrW() - 230), ScrH() - 125, hudtable.colorwhite )
	math.Round(karma)
end
	
//ROUNDTIME
   local is_haste = HasteMode() and round_state == ROUND_ACTIVE
   local is_traitor = LocalPlayer():IsActiveTraitor()

   local endtime = GetGlobalFloat("ttt_round_end", 0) - CurTime()

   local text


   if is_haste then
      local hastetime = GetGlobalFloat("ttt_haste_end", 0) - CurTime()
      if hastetime < 0 then
         if (not is_traitor) or (math.ceil(CurTime()) % 7 <= 2) then
            text = L.overtime
            font = "Trebuchet18"

           local ry = ScrH() - 10
           local rx = ScrW() - (ScrW() - 100)
         else
            text  = util.SimpleTime(math.max(0, endtime), "%02i:%02i")
            color = Color(255,0,0,255)
         end
      else
         local t = hastetime
         if is_traitor and math.ceil(CurTime()) % 6 < 2 then
            t = endtime
          color = Color(255,0,0,255)
         end
         text = util.SimpleTime(math.max(0, t), "%02i:%02i")
      end
   else
      text = util.SimpleTime(math.max(0, endtime), "%02i:%02i")
   end
   draw.RoundedBox(4,ScrW() - (ScrW() - 10), ScrH() - 40, 140, 30, Color(100,100,100,255))
   draw.SimpleText(text, "ChatFont",ScrW() - (ScrW() - 55), ScrH() - 35, Color(255,255,255,255))

   if is_haste then
      draw.SimpleText(L.hastemode, "ChatFont", ScrW() - (ScrW() - 55), ScrH() - 35)
   end
end

Where exactly is line 20 in your file? Line 20 is a blank spot when I put it into my editor, hinting that you have code before this.

yeah sorry line 20 is local clhealth = tgt:Health

It appears that you need someway to tell if the client is spectating, if he isn’t then tgt is going to return nil because there is no one being observed.

Yes I have ulx installed but its just for the spectators on ttt so they can see the persons health name etc

I was an idiot and noticed it was for ttt spectators after I asked it. What I was going to suggest is completely irrelevant now. :v:

Can anyone else help?

tgt is nil most likely because your not setting Player:SpectateEntity( Entity ) elsewhere in your code.

Also, instead of all those group checks you could use “Player:GetUserGroup()” to return the group the player is a member of :slight_smile:

Would I just need to add Player:SpectateEntity( entity ) to my code?

Your first problem is that you are defining clhealth and clname before the target exists, you need to just remove that local and use tgt:Health() or tgt:Nick() later on. So, just remove those two lines and where clname is put tgt:Nick() and for the health part you have another issue entirely:

[lua]if IsValid(tgt) and tgt:IsPlayer() then
draw.RoundedBox(4, ScrW() - (ScrW() - 40), ScrH() - 100, 300 , 30, Color(0,0,0,255))
draw.RoundedBox(4, ScrW() - (ScrW() - 40), ScrH() - 100, health * 3 , 30, Color(200,0,0,255))
draw.RoundedBox(4, ScrW() - (ScrW() - 40), ScrH() - 100, health * 3 , 20, Color(255,0,0,255))
draw.SimpleText(health, “Trebuchet24”, ScrW() - (ScrW() - 165), ScrH() - 98, hudtable.colorwhite )
else
draw.RoundedBox(4, ScrW() - (ScrW() - 40), ScrH() - 100, 300 , 30, Color(0,0,0,255))
draw.RoundedBox(4, ScrW() - (ScrW() - 40), ScrH() - 100, health * 3 , 30, Color(200,0,0,255))
draw.RoundedBox(4, ScrW() - (ScrW() - 40), ScrH() - 100, health * 3 , 20, Color(255,0,0,255))
draw.SimpleText(clhealth, “Trebuchet24”, ScrW() - (ScrW() - 165), ScrH() - 98, hudtable.colorwhite )
end[/lua]

This is your problem, you are saying, if the target exists then draw your OWN health, if he doesn’t exist draw THE TARGETS health. Obviously, if he doesn’t exist it will return nil. I think this should work:

[lua]if IsValid(tgt) and tgt:IsPlayer() then
draw.RoundedBox(4, ScrW() - (ScrW() - 40), ScrH() - 100, 300 , 30, Color(0,0,0,255))
draw.RoundedBox(4, ScrW() - (ScrW() - 40), ScrH() - 100, health * 3 , 30, Color(200,0,0,255))
draw.RoundedBox(4, ScrW() - (ScrW() - 40), ScrH() - 100, health * 3 , 20, Color(255,0,0,255))
draw.SimpleText(tgt:Health(), “Trebuchet24”, ScrW() - (ScrW() - 165), ScrH() - 98, hudtable.colorwhite )
else
draw.RoundedBox(4, ScrW() - (ScrW() - 40), ScrH() - 100, 300 , 30, Color(0,0,0,255))
draw.RoundedBox(4, ScrW() - (ScrW() - 40), ScrH() - 100, health * 3 , 30, Color(200,0,0,255))
draw.RoundedBox(4, ScrW() - (ScrW() - 40), ScrH() - 100, health * 3 , 20, Color(255,0,0,255))
draw.SimpleText(health, “Trebuchet24”, ScrW() - (ScrW() - 165), ScrH() - 98, hudtable.colorwhite )
end[/lua]

Thank you sooo much sir! it worked.

Okay another quick question bit off topic of the spectator hud but I have karma displayed on my hud and when I throw an incen at an innocent it puts my karma to like 958.819246128491 ive tried rounding it but it doesn’t work. Here is my code:


//KARMA
if IsValid(tgt) and tgt:IsPlayer() then
	draw.SimpleText(tgt:GetBaseKarma(), "ChatFont", ScrW() - (ScrW() - 300), ScrH() - 85, hudtable.colorwhite )
	draw.SimpleText("Karma = ", "ChatFont", ScrW() - (ScrW() - 230), ScrH() - 85, hudtable.colorwhite )
	math.Round(karma)
	math.Round(tgt:GetBaseKarma())
else
	draw.SimpleText(karma, "ChatFont", ScrW() - (ScrW() - 300), ScrH() - 85, hudtable.colorwhite )
	draw.SimpleText("Karma = ", "ChatFont", ScrW() - (ScrW() - 230), ScrH() - 85, hudtable.colorwhite )
	math.Round(karma)
end

Calling math.Round by itself won’t actually change the value, you need to assign what it returns.
You need to do
[lua]karma = math.Round(karma) --i dont recommend this as it changes the value[/lua]
OR
[lua]draw.SimpleText(math.Round(karma), “ChatFont”, ScrW() - (ScrW() - 300), ScrH() - 85, hudtable.colorwhite ) --this way you just draw the rounded value without storing it[/lua]