Entity:IsValid
Description
Returns whether the entity is a valid entity or not.
An entity is valid if:
- It is not a NULL entity
- It is not the worldspawn entity (game.GetWorld)
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
Example
Shows how to use the global IsValid function instead of using this method directly.
Example
Output: Outputs 'true' to the console if the player is in-game.
Example
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.