To preface, I know you can do :FindFirstChild() and put an if statement if child is found, then assign the variable that the script had just confirmed exists… Then put an else statement if it isn’t found and return nil.
BUT (HUGE, GIANT BUT)
I’m trying to see if I can do something like this:
Why? Because I’m lazy and I’ve found so many comical ways to shorten scripts in ways that are so ridiculous I slam my head on my desk for not thinking about it sooner.
The only situation where or nil runs is if the first variable is false. So what you’re doing there is only useful if you want to convert false to nil. More specifically:
(X or nil) == X --if X is truthful, for example an Instance, a number, or true
(nil or nil) == nil --no conversion here, nil remains nil
(false or nil) == nil --the only scenario where the first variable changes to nil
Most often than not, a property that isn’t set, or an instance that isn’t found through FindFirstChild, returns nil anyway. Which means that or nil is always redundant unless you specifically want false to convert to nil.
Dang, close - But I’m trying to get the script to not go ‘X is not valid member of Y’ without an if statement.
It’s the stupidest thing ever now that I think about it but I’m also looking for revolutionary ways to write things stupidly. (edit: and have it work through sheer will)
This error happens for instance properties or members that don’t exist(table keys that don’t exist return nil). An easy way to check if a property exists is to wrap it in a pcall:
local function safeGet(x: Instance, property: string): any
local exists, response = pcall(function() return x[property] end)
return exists and response or nil
end
print(safeGet(workspace, "Name")) --Workspace
print(safeGet(workspace, "Test")) --nil
For instance members specifically(other instances parented under them, not properties), FindFirstChild prevents the error(by returning nil).
For instance properties specifically, the new ReflectionService may interest you. It provides functions for inspecting all the properties of a class (you can pass x.ClassName to it and compare with property). Although it can be a bit tricky to determine which of the properties that it returns can be actually read through indexing (without raising errors).
A pcall returns true and the response that the function passed to it returns, or false and the error that the function caused. If the instance x doesn’t have the property property, the error the author mentions will be raised. This will be caught by the pcall, so the pcall will return false and the error. If however property does exist, then the pcall is successful, and it will return what we tell it too (in this case the value of x[property] so the value of property for the instance x).
What the logic statement below the pcall does is equivalent to this:
if pcallSuccessful then
return valueOfPropertyForX
else
return nil
end
If we simply returned exists then the function would tell you if the property exists but not its value, and if we returned response it would return string errors instead of nil if the property doesn’t exist. So the logic statement combines them to create the behavior the author is describing (fetch the value if it exists, else fetch nil instead of error).