It depends on the situation. Typically, it isn't necessary; to quote the Lua reference manual (2.4.4):
Both false and nil are considered false. All values different from nil and false are considered true (in particular, the number 0 and the empty string are also true).
This means that if false then
is equivalent to if nil then
and if workspace:FindFirstChild("Pie") then
is equivalent to if true then
(assuming workspace.Pie exists; if you're not sure if it exists, you should use workspace:FindFirstChild("Pie") instead of workspace.Pie so that it returns nil in case it doesn't exist, instead of erroring).
As such, the following two pieces of code are equivalent, too:
1 | pie = workspace:FindFirstChild( "Pie" ) |
1 | pie = workspace:FindFirstChild( "Pie" ) |
Using not pie
would technically being equivalent to using the expression pie == nil or pie == false
, which comes down to the same thing as pie == nil
if FindFirstChild is used to get the value.
Imagine you want a function that prints the argument unless no argument was given. You could do this:
Converting the ~= nil
check would turn it into this:
If you were to use foo("banana")
and bar("banana")
, they would both print "banana". If you used foo()
and bar()
, neither of them would print anything, as expected.
However, if you were to use foo(false)
and bar(false)
, then foo would print "false", while bar would print nothing.
Aside from this subtle difference, they come down to the same thing though.