What's Wrong with This Script?


include("shared.lua")

function DrawInformation()
	local tr = LocalPlayer():GetEyeTrace();
	if not ( tr.Entity:GetClass() == "money_printer_upgrade" ) then return end; 
	
	local w = 84
	if IsValid( tr.Entity ) and tr.Entity:GetPos():Distance( LocalPlayer():GetPos() ) < 100 then
		draw.RoundedBox( 4, ScrW() / 2 - ( w / 2 ), ScrH() / 2 - 40, w, 20, Color( 255, 255, 255, 100 ) ); 
		draw.SimpleText( "Printer Upgrade", "DermaDefault", ScrW() / 2, ScrH() / 2 - 30, color_black, 1, 1 );
	end;
end;

function ENT:Draw()
	self.Entity:DrawModel()
	DrawInformation()
end

hook.Add( "HUDPaint", "KUpgradeNotif", DrawInformation );

So, I’m having problems with this script in my DarkRP server. Could anyone tell me what’s wrong with it? Here’s the error message.



[ERROR] addons/darkrpmodification/lua/entities/k_printer_upgrade/cl_init.lua:5: Tried to use a NULL entity!
  1. GetClass - [C]:-1
   2. fn - addons/darkrpmodification/lua/entities/k_printer_upgrade/cl_init.lua:5
    3. unknown - addons/ulib/lua/ulib/shared/hook.lua:105

Basically above loops over and over whenever I noclip out of the world.

Use

Global.IsValid and check if tr.Entity is valid

How would I know that it is valid, I’m new to Lua… So, would I just put that in the script and look on console to see if it printed what it returned?

[editline]24th July 2015[/editline]

Would this work?


print( IsValid( tr.Entity ) )


if IsValid( tr.Entity ) then
 -- code
end

I should’ve known that lmao. thanks. checking now.

[editline]24th July 2015[/editline]

It’s coming as an error.



[ERROR] addons/darkrpmodification/lua/entities/k_printer_upgrade/cl_init.lua:14: attempt to index global 'tr' (a nil value)
  1. unknown - addons/darkrpmodification/lua/entities/k_printer_upgrade/cl_init.lua:14


You have to put it after the “local tr…”
It would look something like this
include(“shared.lua”)


function DrawInformation()
	local tr = LocalPlayer():GetEyeTrace();
        if IsValid( tr.Entity ) then
	        if not ( tr.Entity:GetClass() == "money_printer_upgrade" ) then return end; 
        end
	
	local w = 84
	if IsValid( tr.Entity ) and tr.Entity:GetPos():Distance( LocalPlayer():GetPos() ) < 100 then
		draw.RoundedBox( 4, ScrW() / 2 - ( w / 2 ), ScrH() / 2 - 40, w, 20, Color( 255, 255, 255, 100 ) ); 
		draw.SimpleText( "Printer Upgrade", "DermaDefault", ScrW() / 2, ScrH() / 2 - 30, color_black, 1, 1 );
	end;
end;

function ENT:Draw()
	self.Entity:DrawModel()
	DrawInformation()
end

hook.Add( "HUDPaint", "KUpgradeNotif", DrawInformation );]

This worked, mate! Thanks soooo much! :slight_smile: