Nil: Difference between revisions

From Legacy Roblox Wiki
Jump to navigationJump to search
>Outofspace
→‎Uses: - Have you read the book, Nil Zone?
>Outofspace
Line 26: Line 26:
Here are more examples of "nil" usage:
Here are more examples of "nil" usage:


{{Example|1=<i></i>
<pre>
<pre>
game.Workspace.USERNAME.Humanoid.Health = nil
game.Workspace.USERNAME.Humanoid.Health = nil
</pre>
</pre>
}}


Using the script above will have the same effects as the following:
{{Example|1=<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.USERNAME.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 you will gain a wipeout.

Revision as of 22:40, 13 October 2008

Introduction

"Nil" is a noninitialized value, or lack of a value, that is blank and equal to nothing. Arithmetic 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.

Uses

"Nil" can be used to remove the value of a property of an object. It is very common to set the parent of an object to "Nil", removing it from the game per se. If the object is referenced by a variable for example, you can bring the object back.

Example
Example script of referencing and object.
p = Instance.new("Part") -- We will create a new grey brick
p.Parent = game.Workspace -- The part (a grey brick) has a parent, game.Workspace
p.Parent = nil -- The part now has no parent (and so it disappears).
p.Parent = game.Workspace -- part still exists because it is referenced by 'p'
p.Parent = nil -- part disappears again (for 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.USERNAME.Humanoid.Health = nil


Example
Using the script above will have the same effects as the following script:
game.Workspace.USERNAME.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 you will gain a wipeout.

See Also

Absolute beginner's guide to scripting

Parts

Script Creation Walkthrough

Programming in Lua:1.2 - Global Variables

Programming in Lua:2.1 - Nil