Feature Request to have a method for finding the child of an object that might exist and might not exist

I don’t have access to the studio feature request section, probably cause I don’t use the Devforum too often, which is annoying, so this seems like the next best place to put this.

Anyway, I often run into a problem where an object I am trying to get doesn’t exist and returns nil but that also makes any modifiers or functions for the object not work, which errors my script and breaks it. It would be nice if :FindFirstChild() worked and just returned nil for nil without erroring so that situations like the following would not break/stop sections of my code.

this is an example of what breaks the code if the first object is deleted:

Workspace:FindFirstChild("Object 1"):FindFirstChild("Object 2")

if Object 1 Stops existing then it breaks and gives an error saying “attempt to index nil with ‘FindFirstChild’”

A Solution to this is the following:

if Workspace:FindFirstChild("Object 1") then
	if Workspace:FindFirstChild("Object 1"):FindFirstChild("Object 2") then
		-- code here
	end
end

Which works Fine, but it takes A bit more space, especially with more objects inside of each other and is messier.

1 Like

This would be difficult and/or impractical to implement. FindFirstChild is a specific method for Instances, nil is not an instance; it’s a value that represents nothing.

In order for your feature request to work, Roblox would have to explicitly check if something called “FindFirstChild” is being used on a nil value and then return nil. Or, Roblox could just simply return nil for any method that’s called on nil. However, that would likely lead to weird behaviors since you shouldn’t be able to call methods from a value that doesn’t exist.

This would make your code look like this:

local var = nil:GetPropertyChangedSignal("prop") -- this wouldn't error
var:Connect(function() -- still wouldn't error
   --...
end)

-- code works but no visible problem

Although it may be annoying, but the current behavior is better since it gives you an easier and direct way to debug a problem you have, instead of silently closing the problem.

it does not necessarily have to be :FindFirstChild(). It would be nice if there was just simply a way for me to get the child of something if it exists and if it doesn’t exist, say that the child i am looking for does not exist either. A lot of my creations don’t rely on static objects created in studio, but instead objects that may or may not exist that have possibly been created or destroyed by a player, And it is a bit annoying when my code breaks because an object I was trying to find the child of no longer exists.