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.
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.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
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.
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.
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.
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.