prop spawning problem

ok so i set up this function that should spawn a prop with the model according to the table props that i set up further on, but when the prop spawns it spawns right above me like in my head and it doesn’t collide with me or the physics gun. i know it has something to do with the setpos though.

[lua]
function spawnprop( ply, props )
local ent=ents.Create(“prop_physics”)
ent:SetModel( props )
ent:SetPos( ply:GetShootPos() )
ent:SetOwner( ply )
ent:Spawn()
ent:EmitSound(“npc/scanner/combat_scan5.wav”)
end
[/lua]

and later on i do a if then thing where it will run this function with props[1] defined in a table.
i put the whole script in garrysmod/garrysmod/lua/autorun

How can i make it solid to me and physics gun and spawn it at my crosshair?

It doesn’t collide with you because of the SetOwner function. That’s not what SetOwner does, check it out on the Wiki.

As for not working with the physgun, I’m not sure about that.

[lua]ent:SetPos( ply:GetEyeTrace( ).HitPos )[/lua]

Might need to spawn it a bit higher though.

You cant take it because of the setowner too!

You might want to spawn it where you’re looking at.

[lua]function spawnprop( ply, props )

local tr = ply:GetEyeTrace() // Create a trace to where the player is looking
if tr.HitNonWorld then return end // If the player is not looking at the ground then return

local ent=ents.Create("prop_physics")
ent:SetModel( props )
ent:SetPos( tr.HitPos )
ent:Spawn()

ent:EmitSound("npc/scanner/combat_scan5.wav")

end
[/lua]

Oh, late indeed. And Gbps just copy and paste the tabs. :v:

[lua]
function SpawnProps(ply, props)
local tr = ply:GetEyeTrace() // Create a trace to where the player is looking
for k,v in pairs(props) do // For all props models in your table
local prop = ents.Create(“prop_physics”)
prop:SetPos(tr.HitPos)
prop:SetModel(v)
prop:Spawn()
prop:Activate()
prop:SetNWString(“Owner”,ply:Name()) // For SPP which many people have
end
end
[/lua]

Excuse the tabs, I can’t tab in textareas.

thanks, it’s working great! is there any way that i can make it so you cant spawn it if you cursor is hitting a point really far away?

A trace might work, this will make a trace that only goes 256 units in front of you.
[lua]local td = {}
td.start = ply:GetShootPos()
td.endpos = ply:GetShootPos() + ply:GetAimVector()*256
td.fliter = MASK_NPCWORLDSTATIC
local tr = util.TraceLine(td)[/lua]

awesome man, thanx!