Spawn in front of player

I want to spawn an entity right in front of the player’s eyes. I have the spawning working fine, but haven’t got it to spawn in front of the player yet. This is my code to find the postition:


local trace = pl:GetEyeTraceNoCursor()
local sPos = trace.Normal
sPos:Mul( 8 )
sPos = sPos:Add( trace.StartPos )

With pl being the player of course. When I test it, it says that sPos is not a vector and is nil. What have I done wrong?

Try this

local sPos = ply:GetEyeTraceNoCursor():Normalize()*8

GetEyeTraceNoCursor() returns a TraceRes, not a vector though

Ah, right, then use pl:GetAimVector().

This will work.
[lua]
local tr = {}
tr.start = ply:EyePos()
tr.endpos = ply:EyePos() + 95 * ply:GetAimVector()
tr.filter = ply
local trace = util.TraceLine(tr)

local myentity= ents.Create("myentity")
myentity:SetPos(trace.HitPos)
myentity:Spawn()[/lua]

Thanks, I’ll try this now!

Edit:

Worked perfectly! Thanks again!