Nil: Difference between revisions

From Legacy Roblox Wiki
Jump to navigationJump to search
>Camoy
format; removed category
>Ozzypig
Line 1: Line 1:
==Nil==
==Nil==
===What is Nil?===
===What is Nil?===
'''Nil''' is a '''noninitialized''' value, or lack of a value, that is blank and equal to nothing. 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 [[Remove|:Remove()]] function, it is removed from the game completely by setting its parent to 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, or lack of a value that is equal to nothing. 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 [[Remove|:Remove()]] function, 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===


'''Nil''' can be used to remove the value of a property of an object. It is very common to set the [[Parent|parent]] of an object to "Nil", removing it from the game per se. If the object is referenced by a [[Variables|variable]] for example, you can 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|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 [[Variables|variable]], you can set it's Parent property again and bring the object back.


{{Example|''Example script of referencing and object.''
{{Example|''Example script of referencing an object.''
<pre>
<pre>
p = Instance.new("Part") -- We will create a new grey brick
p = Instance.new("Part") -- Create a new brick
p.Parent = game.Workspace -- The part (a grey brick) has a parent, game.Workspace
p.Parent = game.Workspace -- The part (a grey brick, by default) has a parent, the workspace
p.Parent = nil -- The part now has no parent (and so it disappears).
wait(1)
p.Parent = game.Workspace -- part still exists because it is referenced by 'p'
p.Parent = nil -- The part now has no parent (and so it disappears from view, but not from memory).
p.Parent = nil -- part disappears again (for it has no parent)
wait(1)
p.Parent = game.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 garbagecollector
p = nil -- part is no longer referenced by anything, so it gets picked up by the garbagecollector
</pre>
</pre>
Line 24: Line 27:
{{Example|
{{Example|
<pre>
<pre>
game.Workspace.USERNAME.Humanoid.Health = nil
game.Workspace.Player.Humanoid.Health = nil
</pre>
</pre>
<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>
<pre>
<pre>
game.Workspace.USERNAME.Humanoid.Health = 0
game.Workspace.Player.Humanoid.Health = 0
</pre>
</pre>
}}
}}


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, your health will go to 0 and you will gain a wipeout.
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, your health will go to 0 and the humanoid will die.  


===See Also===
==See Also==
 
*[[Absolute beginner's guide to scripting]]
[[Absolute beginner's guide to scripting]]
*[[Part]]
 
*[[Script Creation Walkthrough]]
[[Part]]
*[http://www.lua.org/pil/1.2.html Programming in Lua:1.2 - Global Variables]
 
*[http://www.lua.org/pil/2.1.html Programming in Lua:2.1 - Nil]
[[Script Creation Walkthrough]]
*[http://en.wikipedia.org/wiki/Null_(computer_programming) Null]
 
[http://www.lua.org/pil/1.2.html Programming in Lua:1.2 - Global Variables]
 
[http://www.lua.org/pil/2.1.html Programming in Lua:2.1 - Nil]
 
[http://en.wikipedia.org/wiki/Null_(computer_programming) Null]


[[Category:Data Types]]
[[Category:Data Types]]

Revision as of 13:56, 24 December 2010

Nil

What is Nil?

By definition, nil is a state of non-existence or nothingness. In Lua, nil is a non-initialized value, or lack of a value that is equal to nothing. 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 :Remove() function, 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 it's 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 = game.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 = game.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 garbagecollector


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
game.Workspace.Player.Humanoid.Health = nil

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

game.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, your health will go to 0 and the humanoid will die.

See Also