Garry's Mod Wiki

Entity:IsValid

  boolean Entity:IsValid()

Description

Returns whether the entity is a valid entity or not.

An entity is valid if:

Instead of calling this method directly, it's a good idea to call the global IsValid instead, however if you're sure the variable you're using is always an entity object it's better to use this method

It will check whether the given variable contains an object (an Entity) or nothing at all for you. See examples.

NULL entities can still be assigned with key/value pairs, but they will be instantly negated. See example 3

This might be a cause for a lot of headache. Usually happening during networking etc., when completely valid entities suddenly become invalid on the client, but are never filtered with IsValid(). See GM:InitPostEntity for more details.

Returns

1 boolean
true if the entity is valid, false otherwise

Example

Shows how to use the global IsValid function instead of using this method directly.

if ( entity && entity:IsValid() ) then -- Do stuff end -- The above can be replaced with the following for the same effect (and cleaner code) if ( IsValid( entity ) ) then -- Do stuff end

Example

Output: Outputs 'true' to the console if the player is in-game.

Example

local newPlayer = net.ReadEntity() --server found a new player on the server and sent it to us after print( "1/4" ) -- it determined it was valid (newPlayer is NULL in this realm) if not isnumber(newPlayer.ImportantGameData) then --If it doesn't have a specific field, assign a value to it print( "2/4" ) newPlayer.ImportantGameData = 42 end print( "3/4" ) print( newPlayer.ImportantGameData * 69 ) --Attempting to do arithmetics on the new field print( "4/4" )
Output: 1/4 ... 3/4
  • A lua error telling us 'ImportantGameData' is a nil value and thus can't perform math on it

But this shouldn't be possible, because we just created a value there. No red flags are present up until this point; all code up until this will run fine. Make sure to add an IsValid() check when the seemingly impossible happens.