trace.Entity:GetPhysicsObject() returns an invalid PhysObj

Hi, there forum,

No matter how i get the PhysObj from the trace.Entity it returns an invalid physics object.

I am calling this line in “TOOL:DrawHUD()”, where my trace is

trace = self:GetOwner():GetEyeTrace()

local phy = trace.Entity:GetPhysicsObject()

Console returns: “PhysObject”

after that i can’t call like phy:GetMass()

Tried to use invalid object (type IPhysicsObject) (Object was NULL or not of the right type)

Why is that happening ?

Did you validate it?

Yes i did and that’s the problem …
If i call like

if( trace.Entity:GetPhysicsObject():IsValid ) then
// code 1
// code 2

Only Code 2 will be active at all time …

So wait you are trying to acess the physics object on the client?

That won’t work out.

Look at this article its both sides

So how can i work it out i am thinking to send mass to a CVar and then Read it …

I managed to solve my issue :slight_smile: 10x anyway :slight_smile:

You can’t really use physics objects on the client side. While most sources say it’s shared, there are very few cases where you can do it on the clientside. Your best bet just to 100% safeguard it would be

if ent and IsValid( ent ) and ent.GetPhysicsObject and IsValid( ent.GetPhysicsObject() ) then
 // code here

Try not to use assignment until you’ve validated that it exists and is a proper physics object entity.

10x :slight_smile:

You do realize ent and IsValid( ent ) does exactly the same thing, so instead of checking if ent is not nil, you can just use the function IsValid() which already does it.

Huh. Never knew that, always thought you had to check for variable assignment and validity of the entity. The more you know.

You had to do it for if ent and ent:IsValid()

xD I already made it work:

See the UCS when i am pointing the world :slight_smile: