Spawning props incorrectly

I made a stool that allows a player to spawn certain props. It works the first time, then gives this error on subsequent tries:

weapons\gmod_tool\stools/******.lua:24: bad argument #1 to 'SetPos' (Vector expected, got nil)

Here’s the code (shortened to what’s relevant):
[lua]TOOL.Category = “EMDF”
TOOL.Name = “******”
TOOL.Command = nil
TOOL.ConfigName = “”

ClickPos = Vector(0,0,0) --where the user clicks

–Left click method
function TOOL:LeftClick( trace )
–ClickPos = trace.StartPos
ClickPos = trace.hitPos

if trace.HitNonWorld then return end – If the player is not looking at the ground then return

local ent=ents.Create(“prop_physics”)

ent:SetModel( “<model>.mdl” )
ent:SetPos( ClickPos ) --This is line 24, where the error occurs
ent:SetMoveType(MOVETYPE_NONE) --prevents prop from moving
ent:Spawn() --Spawn prop

[more irrelevant code]
Either it spawns exactly where I wanted it, or it spawns in the air. Earlier, before some tinkering, it spawned in the air. It may have changed to spawning where I want since then. But the problem I’m having is that it only works once. Any ideas on why this is?

Also, I’ve been switching back and forth between “ClickPos = trace.StartPos” and “ClickPos = trace.HitPos”. Which one’s correct? Or is there another vector I need to use to spawn the prop exactly where the user aims the tool gun?

Thanks in advance!

“trace.HitPos” not “trace.hitPos”, I believe those values in the TraceRes are case-sensitive. Otherwise, Why do you need to record the ClickPos in the first place? You also might want to make that a local value. You’ve created a global value.

Everything is case sensitive. That’s the way coding works

Like was said above. its trace.HitPos not trace.hitPos

Not entirely. My only doubt to that was due to having keyvalues, after being translated to and from tables, spelt lowercase.


Also, there is absolutely no reason for you to have to store the value of the HitPos in a local variable or even a global, before using it, unless you’re going to retrieve that information later.

Thanks! That was the problem. Yes, LUA is case sensitive. At least, from what I’ve been seeing. The GMOD 10 Lua plugin for NP++ doesn’t recognize “HitPos” as a valid attribute, so it didn’t raise any red flags (figuratively speaking).

Now all I have to do is mess around with the code some more, and make it spawn the objects where I want them, in the number I want them. Shouldn’t be too hard.