In Lua, the nil value type only has one value: itself. Nil is used to represent a type of nonvalue, meaning that unassigned variables have a default value of nil and operands without a provided argument (e.g. void) evaluate to nil. Nil also represents the boolean false in a logical expression. The literal nil
represents a value of nil.
Nil is often used to remove a reference to an object to leave the object for garbage collection to conserve memory.
Usage[]
When working with variables, assigning nil to the variable, doesn't "destroy" the original value, per se, but rather replaces the original value. Also, when calling a non-existing value, you will get nil as well. Ex:
print(a) --Output: nil
local a = 0
print(a) --Output: 0
a = nil
print(a) --Output: nil
When you attempted to index "a" and print its value at first, you got nil because "a" was yet to defined. Also, when you made "a" nil, you replaced its previous value with nil, but you didn't "destroy" the previous value of 0- it's just that "a" is no longer 0.
When the following code is used,
object:Destroy()
an object and all of its descendants are parented to nil, essentially "deleting" them.
Objects that are parented to nil, can be retrieved if you have a reference to the object. Ex:
object:Destroy()
object.Parent = Workspace
The code above removes object and all of its children to nil, then returns object to the Workspace.
Be warned, that objects that are in nil are garbage collected to conserve memory, meaning that if you don't revive them, they really are gone forever.
Programming |
| ||||||
---|---|---|---|---|---|---|---|
Design | |||||||
Assets | |||||||
Tools | |||||||
Monetization | |||||||
Analytics | Developer Stats ยท Monthly active users (MAU) | ||||||
Advertising | |||||||
Resources |
Basic Lua types | |||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Roblox types |
| ||||||||||||||||||||||||||
API reference only |