Nil

From Goodblox Wiki
Revision as of 10:46, 20 September 2021 by Xyrafrost (talk | contribs) (created the page)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search


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.

local p = Instance.new("Part") -- We will create a new grey brick
p.Parent = workspace -- The part (a grey brick) has a parent, Workspace
p.Parent = nil -- The part now has no parent (and so it disappears).
p.Parent = 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:

workspace.USERNAME.Humanoid.Health = nil

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

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

Null