Toolgun trace returning null entity...

Hi everyone! I’m experimenting with making a stool, and I am having a bit of an issue. Currently, all I’d like it to do is print the angles of the prop I click into the console. Here’s my code:


function TOOL.LeftClick( tr )
	
	tr = util.TraceLine( trace )
	
	local prop = trace.Entity:GetAngles() --line 11
	local propAngles = prop.GetAngles()
	
		print("The angles of the prop you clicked are: " .. tostring(prop))
		
		
end

And here is the console error when I fire it at a prop:



[ERROR] gamemodes/sandbox/entities/weapons/gmod_tool/stools/anglefixer.lua:11: Tried to use a NULL entity!
  1. GetAngles - [C]:-1
   2. LeftClick - gamemodes/sandbox/entities/weapons/gmod_tool/stools/anglefixer.lua:11
    3. unknown - gamemodes/sandbox/entities/weapons/gmod_tool/shared.lua:254


Probably making some silly little mistake, but I cant seem to figure it out. Could someone help me out?

You are generating a new trace from nothing, overriding the trace the tool gun actually gives you.

REMOVE LINE 9

Well, that makes it have a different issue. Here’s the code without line 9:


function TOOL.LeftClick( tr )
	
	PrintTable(tr)
	
	local prop = tr.Entity() --line 11
	local propAngles = prop:GetAngles()
	
		print("The angles of the prop you clicked are: " .. tostring(propAngles))
		
		
end

And here’s the console error it gives when I fire at a prop, along with the PrintTable showing the table values:


AllowedCVar	=	userdata: 0x4114a288
BuildCPanel	=	function: 0x42ce5c60
Category	=	(Construction)
ClientConVar:
ConfigName	=	
DrawToolScreen	=	function: 0x3217f1b0
LastMessage	=	0
LeftClick	=	function: 0x399c2810
Message	=	start
Mode	=	anglefixer
Name	=	AngleFixer
Objects:
Owner	=	[NULL Entity]
RightClick	=	function: 0x42ce59f8
SWEP	=	Weapon [94][gmod_tool]
ServerConVar:
Stage	=	0
Weapon	=	Weapon [94][gmod_tool]

[ERROR] gamemodes/sandbox/entities/weapons/gmod_tool/stools/anglefixer.lua:11: attempt to call field 'Entity' (a nil value)
  1. LeftClick - gamemodes/sandbox/entities/weapons/gmod_tool/stools/anglefixer.lua:11
   2. unknown - gamemodes/sandbox/entities/weapons/gmod_tool/shared.lua:254


As you can see, the toolgun trace doesn’t seem to give any Entity field.

TOOL**:**LeftClick( tr )

Can you even copy paste properly?

It should be



tr.Entity


It should have no parentheses. The Entity field of the TraceResult is an entity object, not a function.

Very nice! That got me past that line, but now its throwing up a different issue. Here’s the new code:


function TOOL.LeftClick( tr )
	
	local prop = tr.Entity
	print("Value of 'prop' is: " .. tostring(prop))
	local propAngles = prop.GetAngles() -- new line 11
	
		print("The angles of the prop you clicked are: " .. tostring(propAngles))
		
		
end

And here’s the console error:


Value of 'prop' is: nil

[ERROR] gamemodes/sandbox/entities/weapons/gmod_tool/stools/anglefixer.lua:11: attempt to index local 'prop' (a nil value)
  1. LeftClick - gamemodes/sandbox/entities/weapons/gmod_tool/stools/anglefixer.lua:11
   2. unknown - gamemodes/sandbox/entities/weapons/gmod_tool/shared.lua:254


The entity value comes up nil even though I clicked a prop. :confused: Why is that?

If you’re talking about how I changed the code, I did because it had some fuck-ups in it.

[lua]
TOOL:LeftClick( tr )
[/lua]
[lua]
TOOL:LeftClick
[/lua]
[lua]
:LeftClick
[/lua]
[lua]
:
[/lua]

I assumed you took Robotboy’s advice there. You need to replace the period in TOOL.LeftClick with a colon, giving you:



function TOOL:LeftClick(tr)


edit: Ninja’d by James.

Isn’t it supposed to be a colon?

**Edit: **Oh shit I guess it isnt xD

**Edit: **I actually don’t know how that got into my first code :confused:

**Edit: **Still doesnt work though, and for some reason, everytime I post a reply to this thread, it posts as an edit to this post…

it is supposed to be a colon

Omg dude, youre right!!! It works!!! I cant thank you enough, that was one of those annoying little mess ups that just ate at me… :dance: