Entity spawn at point player is looking at

I figured this would be a simple script that I could do to test my Lua abilities. I wanted to make a console command (that i could bind to a key) to spawn a nuke wherever i was looking. I wanted to see if I could do it myself, from scratch, but I can’t (yet).

Here’s what I have so far:

function NukeSpawn()
local ent = ents.Create(“sent_nuke”)
ent = sent_nuke
local pos = self.Owner:GetShootPos()
ent:SetPos( pos )
ent:Spawn()

return ent

end

concommand.Add( “nukevision_go”, NukeSpawn() )

I’m saving it in lua\autorun\client
ukevision.lua

Right now I’m getting this error: autorun/client/nukevision.lua:4: attempt to index global ‘self’ (a nil value)

Honestly I don’t know if I’m even on the right track.

“self” is not defined and your concommand values are all wrong.

Fixed code:
[lua]
function NukeSpawn(ply)
local ent = ents.Create(“sent_nuke”)
ent:SetPos(ply:GetShootPos())
ent:Spawn()
end

concommand.Add( “nukevision_go”, NukeSpawn )
[/lua]

It also seems like you are trying to run this script on the client side am i right? Well, to make this work, you need to make the server do this function. Having it under the “client” won’t make it work. Spawning an entity can’t be done by clients joining the server.

I also notive you’re having brackets in your concommad.Add command. Never do that, the earth will implode.
Just leave the name, like this:
[lua]
concommand.Add( “nukevision_go”, NukeSpawn )
[/lua]

Using “self.Owner:GetShootPos()” works, but not the way i think you wanted i believe. It’s a command used to get the position of your gun’s end. Like where the bullets COME from.

And delete the “ent = sent_nuke” line, you don’t need it. It doesn’t do anything.

Hope it helped

Not even. Bullets come from your head. Use player.GetEyeTrace. It returns a trace, so you’ll need to get the hitpos from that.

Thank you everyone. I am now using iRzilla’s code in garrysmod\lua\autorun\server
ukevision.lua but when i run it the server says “unknown command” even though it autocompletes it. When i use lua_openscript, no errors pop up. is it saved in the wrong place? That’s my only other guess (i know it can create sent_nuke, because i do that all the time in console).

Restart gmod, I occasionally get this bug where it doesn’t want to accept any more concommands. No idea why, if it doesn’t work post back here.

Yeah I was just coming to post that it worked this morning. However Chief was right; the nukes are going off in my face. I’ll play with geteyetrace.

[editline]02:20PM[/editline]

Finally got it working with this:

function NukeSpawn(ply)
local ent = ents.Create(“sent_nuke”)
local traceRes = ply:GetEyeTrace()
ent:SetPos(traceRes.HitPos)
ent:Spawn()
end

concommand.Add( “nukevision_go”, NukeSpawn )

Thanks for everyone’s help.