Nil: Difference between revisions

From Legacy Roblox Wiki
Jump to navigationJump to search
>JulienDethurens
No edit summary
m Text replacement - "</SyntaxHighlight>" to "</syntaxhighlight>"
Tags: mobile web edit mobile edit
 
(9 intermediate revisions by 3 users not shown)
Line 2: Line 2:


==What is Nil?==
==What is Nil?==
By definition, {{type|nil}} is a state of non-existence or nothingness. In Lua, {{type|nil}} is a ''non-initialized'' value, or lack of a value that is equal to nothing. It represents the absense of a useful value. Math cannot be performed on {{type|nil}} or anything equivalent to nil, because ''nothing'', unlike 0, is not a numerical value. When an object is deleted, using the [[Destroy (Method)|Destroy]] method, it is removed from the game completely by setting its [[Parent (Property)|Parent]] to {{type|nil}}. Unless the object is referenced, it cannot be recovered.
By definition, '''nil''' is a state of non-existence or nothingness. In Lua, '''nil''' is a ''non-initialized'' value, and is a special value used to represent nothing. It represents the absense of a useful value. Math cannot be performed on nil or anything equivalent to nil, because ''nothing'', unlike 0, is not a numerical value. When an object is deleted, using the [[Destroy]] method, it is removed from the game completely by setting its parent to nil. Unless the object is referenced, it cannot be recovered.


==Use Cases==
==Use Cases==


{{type|Nil}} can be used to blank the value of a property of an object (this can only be done with Instance-type properties; you cannot do this to number properties). It is very common to set the [[Parent]] of an object to {{type|nil}}, which removes it from the game. However, it is possible to bring the object back. For example, if the object is referenced by a [[variable]], you can set its Parent property again and bring the object back.
'''Nil''' can be used to blank the value of a property of an object (this can only be done with Instance-type properties; you cannot do this to number properties). It is very common to set the [[Parent]] of an object to '''nil''', which removes it from the game. However, it is possible to bring the object back. For example, if the object is referenced by a [[variable]], you can set its Parent property again and bring the object back.


{{Example|''Example script of referencing an object.''
{{Example|''Example script of referencing an object.''
<code lua>
<syntaxhighlight lang="lua">
part = Instance.new('Part') -- Create a new brick
p = Instance.new('Part') -- Create a new brick
part.Parent = Workspace -- The part (a grey brick, by default) has a parent, the workspace
p.Parent = Workspace -- The part (a grey brick, by default) has a parent, the workspace
wait(1)
wait(1)
part.Parent = nil -- The part now has no parent (and so it disappears from view, but not from memory).
p.Parent = nil -- The part now has no parent (and so it disappears from view, but not from memory).
wait(1)
wait(1)
part.Parent = Workspace -- part still exists because it is referenced by the variable 'p'
p.Parent = Workspace -- part still exists because it is referenced by the variable 'p'
wait(1)
wait(1)
part.Parent = nil -- part disappears again (it has no parent)
p.Parent = nil -- part disappears again (it has no parent)
part = nil -- part is no longer referenced by anything, so it gets picked up by the garbage collector
p = nil -- part is no longer referenced by anything, so it gets picked up by the garbage collector
</code>}}
</syntaxhighlight>}}


Look through the comments in the script above to understand why recovering an object by reference is possible.
Look through the comments in the script above to understand why recovering an object by reference is possible.


Here are more examples of {{type|nil}} usage:
Here are more examples of "nil" usage:


{{Example|<code lua>
{{Example|<syntaxhighlight lang="lua">
Workspace.Player.Humanoid.Health = nil
Workspace.Player.Humanoid.Health = nil
</code>
</syntaxhighlight>
<i>Using the script above will have the same effects as the following script:</i>
<i>Using the script above will have the same effects as the following script:</i>
<code lua>
<syntaxhighlight lang="lua">
Workspace.Player.Humanoid.Health = 0
Workspace.Player.Humanoid.Health = 0
</code>}}
</syntaxhighlight>}}


This occurs because setting a players health to {{type|nil}} will cause the player to have nothing as a health. Because nothing is similar to having no health at all, you will die.  
This occurs because setting a players health to "nil" will cause the player to have nothing as a health. Because nothing is similar to having no health at all, you will die.  


==See Also==
==See Also==
Line 43: Line 43:
*[http://en.wikipedia.org/wiki/Null_(computer_programming) Null]
*[http://en.wikipedia.org/wiki/Null_(computer_programming) Null]


[[Category:Data Types]]
[[Category:Data types]]
[[Category:Scripting Tutorials]]
[[Category:Scripting Tutorials]]

Latest revision as of 06:02, 27 April 2023

What is Nil?

By definition, nil is a state of non-existence or nothingness. In Lua, nil is a non-initialized value, and is a special value used to represent nothing. It represents the absense of a useful value. Math cannot be performed on nil or anything equivalent to nil, because nothing, unlike 0, is not a numerical value. When an object is deleted, using the Destroy method, it is removed from the game completely by setting its parent to nil. Unless the object is referenced, it cannot be recovered.

Use Cases

Nil can be used to blank the value of a property of an object (this can only be done with Instance-type properties; you cannot do this to number properties). It is very common to set the Parent of an object to nil, which removes it from the game. However, it is possible to bring the object back. For example, if the object is referenced by a variable, you can set its Parent property again and bring the object back.

Example
Example script of referencing an object.
p = Instance.new('Part') -- Create a new brick
p.Parent = Workspace -- The part (a grey brick, by default) has a parent, the workspace
wait(1)
p.Parent = nil -- The part now has no parent (and so it disappears from view, but not from memory).
wait(1)
p.Parent = Workspace -- part still exists because it is referenced by the variable 'p'
wait(1)
p.Parent = nil -- part disappears again (it has no parent)
p = nil -- part is no longer referenced by anything, so it gets picked up by the garbage collector


Look through the comments in the script above to understand why recovering an object by reference is possible.

Here are more examples of "nil" usage:

Example
Workspace.Player.Humanoid.Health = nil

Using the script above will have the same effects as the following script:

Workspace.Player.Humanoid.Health = 0


This occurs because setting a players health to "nil" will cause the player to have nothing as a health. Because nothing is similar to having no health at all, you will die.

See Also