Valid entity check returns false on the client

So forum, I was coding TA the other day when I got a message from my system that the trace is not valid in these three lines:

It’s like the IsValid function/method is not working because in the “print” statement I get { true, [91]prop_physics, true, true }

      local trEnt = actTr.Entity; print(actTr.Hit, trEnt, trEnt:IsValid(), IsValid(trEnt))
      if(not (trEnt and trEnt:IsValid())) then -- This here made me furious yesterfay
      return asmlib.StatusLog(nil,"PHYSGUN_DRAW: Trace entity invalid") end

I tried to delete the addon cache, validate Gmod cache files, but with no success whatsoever.

I am hooking the function to “PostDrawHUD” hook:

Any idea why IsValid is not working ?

World is not a valid entity.

[editline]13th September 2016[/editline]

Just read it goddamnit.

I am not hitting the world. I am hitting Entity [91] prop_physics ( look at the monorail beam screenshot )

I am holding the left mouse button pressed, then pointing to the prop ( Entity [91] ) and then the world

The source is:

Jesus Christ…


Well, it sais that while I am pointing to the entity with the Physgun if that can help.

Your problem makes no sense. First of all, you have a bunch of useless nil checks all over your code: TraceWhatever will always return a table with a key “Entity” guarenteed to be an entity or NULL. Your debug print is correct. If you want to include the world in your entity code done later, then check if trEnt is NULL. Otherwise, check trEnt:IsValid(), which returns false for the world.

[editline]13th September 2016[/editline]

Also, IsValid(var) just checks if the variable is nil, has the IsValid method implemented, and then calls var:IsValid(). Really, this is extra “safety” for entities and does the same thing as :IsValid().

The nil checks are for my library. Sometimes when I test the code, I do not prefer to restart the whole game just to test something small :wink:

Why everyone is concluding that I need the world here. I don’t. I asked different question. :smiley:

Why when I hit this monorail track IsValid sais that it is not valid. That’s it. ( Already tried with IsValid(ENT) too, it has the same result… invalid entity )

you not asked world but you asked why false. Thats why they detail worldspawn is always false on (IsValid()) client side. Because you check it on clientside.

May be I am missing something then, I coded this at 00:10 at night :smiley: Thanks I will try to figure this out …

[lua]print(ent and (ent:IsValid() and ent:IsWorld())[/lua]
The code above has an intentional stupid mistake. Fix it and it will solve your problem, or if you can’t… well, not my problem.

Wow Robot, I’m seeing your modified picture for the first time now :D.

Anyways, I forgot to replace one trace down in the code, and this was the main reason why this was happening. The IsValid() enitity method is 100% OK.

What did I learned today…

Avoid coding late at night …

Now the modification works at 100% with some modifications that speed the tool up :S

Thanks all ! Closing this as solved.