Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

What is Nil?

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

✓ Accepted answer

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.

2 comments

0

so like If I had something like int.Value = 1 and then wanted it to be nothing, I could do int.Value =nil?

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)

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.

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.

1 comment

0

why did this get -1, it's a proper answer lol

This discussion is preserved as it was. New answers, comments, and votes are closed.