Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

What is Nil?

Asked by 8 years ago

So I have seen many scripts that have Nil in them, but up until now I wanted to know, what the heck is Nil, so I came here, any help at all would be nice.

3 answers

Log in to vote
4
Answered by 8 years ago

Nil

Nil is nothing. Nothing, nada, nowt. It represents no value, and is its own datatype.

  • nil is able to be used to clear a value. It's also the default value for empty entries in tables.
  • nil holds no reference.
  • nil is defaultly returned by functions if they do not explicitly return anything.
  • nil is a keyword, but technically you could use just about any undefined variable to represent it.
0
so like If I had something like int.Value = 1 and then wanted it to be nothing, I could do int.Value =nil? Conmmander 479 — 8y
0
Yep. But be careful about what int is, because IntValues may not take kindly to you setting their Value to nil (You'd be better off setting it to 0 in that case) User#6546 35 — 8y
Ad
Log in to vote
1
Answered by
dyler3 1510 Moderation Voter
8 years ago

The term nil basically just means "nonexistent" or 0. In programming terms, it is used to for a few different things which I'll go through below


Placeholder

You may have seen things in scripts like this:

local a = nil

In this instance, nil is basically just serving as a placeholder, for the variable a, so that further into the code you can come back and give a an actual value.

This can be useful in using local variables which may need to be declared, but given a value in different scopes.


Functions

nil can be also be used in returning values in functions. An example would be something like this:

function Check()
    part = game.Workspace:FindFirstChild("Part")
    if  part then
        return part.Name
    end
end

print(Check()) --Prints the parts name if it exists, otherwise it prints nil

As you can see, the function goes through and checks to see if a part exists, and if it does, it returns the part to the function. Otherwise, if the part is not found, it returns nil, or nothing.

The use of this functionality isn't much different to that of using it for holding blank variables.


Clearing

The last use for nil is simply clearing variables. nil can be used to take the value away from any type of variable, and set it to a blank slate that can then be tinkered with in any way you want, like so:

a = 5
a = nil --Removes the value from 'a'

Anyways, I hope this helped. If you have any further problems/questions, please leave a comment below.

Log in to vote
-1
Answered by
wackem 50
8 years ago

First of all, nil means non-existent, but can be set to a value later, though RBXLua doesn't have null in it's vocabulary, null means non-existent and CAN NOT be set to a value afterwards, just thought I'd throw it in there in case you see it in other languages.

0
why did this get -1, it's a proper answer lol wackem 50 — 8y

Answer this question