Addon Lua Script - attempt to call method 'GetName' (a nil value)

I have a lua script inside an addon auto run file, but it gives me this error:

[Simple UI] lua/autorun/hud.lua:31: attempt to call method ‘GetName’ (a nil value)

  1. v - lua/autorun/hud.lua:31
  2. unknown - lua/includes/modules/hook.lua:84

My code:



local player = LocalPlayer()

surface.CreateFont( "DefaultFont", {
	font = "Verdana",
	extended = false,
	size = 15,
	weight = 500,
	blursize = 0,
	scanlines = 0,
	antialias = true,
	underline = false,
	italic = false,
	strikeout = false,
	symbol = false,
	rotary = false,
	shadow = false,
	additive = false,
	outline = false,
} )

hook.Add("HUDPaint", "", function()
	local health = LocalPlayer():Health()
	if health > 60 then
		colorhealth = Color(50,255,50)
	elseif health > 20 then
		colorhealth = Color(230,230,60)
	else
		colorhealth = Color(250,50,50)
	end
	draw.RoundedBox(0,10,ScrH() - 160,220,150,Color(50,50,50, 150))
	if player:GetName() ~= nil then --LINE 31
		draw.SimpleText(player:GetName():upper(), "DefaultFont",20,ScrH()-150,Color(255,255,255,150),0,0)
	end
	draw.RoundedBox(0,20,ScrH()- 130,200,15,Color(30,30,30))
	draw.RoundedBox(0,20,ScrH()- 130,health * 2,15,colorhealth)
	local kills = LocalPlayer():Frags()
	draw.SimpleText("Kills: "..kills,"DefaultFont",20,ScrH()- 100,Color(255,255,255,150),0,0)
	local deaths = LocalPlayer():Deaths()
	draw.SimpleText("Deaths: "..deaths,"DefaultFont",100,ScrH()- 100,Color(255,255,255,150),0,0)
	draw.SimpleText(tostring(player:GetActiveWeapon():GetPrintName():upper()),"DefaultFont",20,ScrH()- 60,Color(255,255,255,150),0,0)
	local ammo1 = player:GetActiveWeapon():Clip1()
	local ammo2 = player:GetActiveWeapon():Clip2()
	if ammo1 >= 0 and ammo2 >= 0 then
		draw.SimpleText("Ammo: "..ammo1.."/"..ammo2,"DefaultFont",20,ScrH()- 40,Color(255,255,255,150),0,0)
	elseif ammo1 >= 0 then
		draw.SimpleText("Ammo: "..ammo1,"DefaultFont",20,ScrH()- 40,Color(255,255,255,150),0,0)
	end
end)

local hide = {
	CHudHealth = true,
	CHudBattery = true,
	CHudAmmo = true,
	CHudSecondaryAmmo = true
}

hook.Add( "HUDShouldDraw", "HideHUD", function( name )
	if ( hide[ name ] ) then return false end
end )

Also, do addons run on the server or client and I tried fixing it with an if statement. This script works fine when I do lua_openscript_cl huddraw.lua

Try using

LocalPlayer():Nick() instead of Player:GetName()

Also, it is good practice to name your hooks instead of leaving them blank. (that second argument after the HUDPaint is defined as the hook)

This file uses client-only hooks, so yes, it will only run clientside. (lua/autorun/client - or wherever in your gamemode clientside only files are ran)

Look at the top bit of it. It is already defined as local player at the top.
You are right on the fact it should be Player:Nick() and not GetName()

Yeah, I edited that right away.

Another thing I just noticed is that he’s using player:GetName():upper() which should be:



string.upper( LocalPlayer():Nick() )


Thanks, Ill give it a try

File location must be lua/autorun/client, not lua/autorun (its shared). Also, your hook has no name.

:upper() and :lower() aren’t incorrect, that part’s fine

GetName will always return a string. Strings have a metatable, thus methods can be called that way.

Same error, just with Nick()

[editline]3rd December 2016[/editline]

I also put it into the client folder

Change player to LocalPlayer()

To add onto this, the client isn’t valid when the file is ran, as such it returns a NULL entity.

Should I run some code at the top of the script to repeat until the data isn’t nil? And how would I do this?

Just call LocalPlayer in the hook, don’t localise it in the file.

How would I do this? I only started yesterday

You already did it on the first line of your HUDPaint hook. Alternatively, you can just do


local player = LocalPlayer()

On the first line and replace your LocalPlayer() calls with player.

The problem is that the players properties try and load before they load in and are nil, but I don’t know how to fix this and I don’t understand what you are saying

[editline]3rd December 2016[/editline]

I tried putting
if player:Health() and player:Nick() and player:Frags() and player:Deaths() and player:GetActiveWeapon():Clip1() and player:GetActiveWeapon():Clip2() then

at the start of the hud and it gives an error of:
lua/autorun/client/hud.lua:23: Tried to use a NULL entity!

Put the code I provided in the previous post on the first line of the hook function. Remove the if statement you posted.

Thanks, it works now! Could you give me an explanation of why it works?

If your trying to find the name of a unknown entity or just not the player entity. Do



ent:GetClass() -- This returns a string of the entity class.


Because the Lua script is loaded before the game “starts up” fully and all the entities (things like props and players) are initialized. When you run local player = LocalPlayer() at the top of the file, the player entity is not yet initialized so the function returns nil (no value). In HUDPaint, however, the player DOES exist and is initialized, so LocalPlayer() returns the correct value.