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
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!