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

What does ~= nil mean, how can I use it, is there alternitaves?

Asked by
2mania 14
1 year ago

I was watching a scripting tutorial and I know what nil means but what does it mean in this sentence and how can I use it in code?

1 answer

Log in to vote
1
Answered by 1 year ago
Edited 1 year ago

The operators ~ (not) and = (equal) is checking whether something is not equal to something else.

For example, if you were using the FindFirstChild method to find a particular child, the FindFirstChild method would either return the first child it found with the given string, or, it would return nil if it had not found a child with the given string.

It is important to check whether the FindFirstChild method returned something that is not nil; if the method did return nil, it would error later on in the script.

local child = workspace:FindFirstChild("Part")

if child ~= nil then
    print("child exists")
else
    print("child does not exist")
end

Another way of doing this is to exclude the ~= nil part; it will still check if child is not nil or false.

local child = workspace:FindFirstChild("Part")

if child then
    print("child exists")
else
    print("child does not exist")
end
0
imagine "(not not child) == true" ???? TheeDeathCaster 2368 — 1y
Ad

Answer this question