Spawning a prop_physics via ConsoleCommand (Last resort question)

This has been solved, not sure how to close the thread if possible. But if needed, lock this please.

I know it may seem like a simple question, but I spent 3+ hours trying my best to fix something else and now I have ran into this.

function SpawnProp( spawner, mdl )
local ent = ents.Create(“prop_physics”)

local trace = {}
trace.start = spawner:GetShootPos()
trace.endpos = trace.start + (spawner:GetAimVector() * 1024)
trace.filter = spawner
local tr = util.TraceLine( trace )

ent:SetPos( tr.HitPos )
ent:SetModel( mdl )


local entphys = ent:GetPhysicsObject()
if entphys:IsValid() then
Msg("Weight of spawned prop: "..entphys:GetMass().."

Msg("Model of spawned prop: “…ent:GetModel()…”
concommand.Add(“SpawnProp” , SpawnProp )

I run this via vgui “SpawnProp localPlayer() <model>” using runConsoleCommand.

Now for my error(s):
[@init.lua:273] Tried to use invalid object (type IPhysicsObject) (Object was NULL or not of the right type)
Problem: Not exactly sure what is wrong with the function, I just need someone to check it for me so I know I did it wrong and where or if the function itself is messed up.

As I have in the title, I have tried to fix this many of times. This is my last resort, I don’t want to give up but I feel I can’t figure this out on my own.

Thank you for your time.

[lua]function SpawnProp( ply, command, args )[/lua]
args[1] would get the model

Could you explain a little more in depth on what “args” does when you insert that into the function? I have no idea what it does to be completely honest. Thanks.

If you put ‘SpawnProp (model) (example)’ in console, args[1] would be the model and args[2] would be (example)

I kinda understand now that I have seen what the wiki says. This makes it a little more clear, I will change it up and report back with the top post. Thank you techtuts0.

It works perfectly now with no problems. Thank you so much for sparing me 3+ more hours struggling to find this.

There’s already a command to spawn props, prop_physics_create/prop_dynamic_create. I know it’s not what you’re looking for, and it only works on props that are defined as prop_physics in their qc, and spawning physics props with prop_dynamic_create crashes, but I’m just throwing it out there.