How to avoid nesting code

I frequently need to confirm that things exists before executing code.

Most times I can just use the not condition, like so:

if not Location:FindFirstChild("Store") then return end
if not Location.Store:FindFirstChild("Shelves") then return end
if not Location.Store.Shelves:FindFirstChild("Cans") then return end
if not Location.Store.Shelves.Cans:FindFirstChild("Beans") then return end
if not Location.Store.Shelves.Cans.Beans:FindFirstChild("Green") then return end
if not Location.Store.Shelves.Cans.Beans.Green:FindFirstChild("String") then return end

But, inside a for loop this does not work. It stops the loop.
So, I have to nest the statements like so:

if Location:FindFirstChild("Store") then
	if Location.Store:FindFirstChild("Shelves") then
		if Location.Store:FindFirstChild("Cans") then
			if Location.Store.Cans:FindFirstChild("Beans") then
				if Location.Store.Cans.Beans:FindFirstChild("Green") then
					if Location.Store.Cans.Beans.Green:FindFirstChild("String") then
						print("Found Green String Beans")
					end
				end
			end
		end
	end
end

I want to avoid nesting the statements like that.
I tried using continue but it doesn’t work.

if Location:FindFirstChild("Store") then continue end

Does anyone know how to do this?

1 Like

you still have to negate the guard clause:

if not Location:FindFirstChild("Store") then continue end

if not Location.Store:FindFirstChild("Shelves") then continue end

if not Location.Store:FindFirstChild("Cans") then continue end

if not Location.Store.Cans:FindFirstChild("Beans") then continue end
				
if not Location.Store.Cans.Beans:FindFirstChild("Green") then continue end

if not Location.Store.Cans.Beans.Green:FindFirstChild("String") then continue end

print("Found Green String Beans")

Please just use FindFirstDescendant(), or even better, FindFirstChild(name, true)

local Beans = Location:FindFirstChild("Beans", true)

if not Beans then continue end
				
if not Beans:FindFirstChild("Green") then continue end

if not Beans.Green:FindFirstChild("String") then continue end

print("Found Green String Beans")
1 Like

Roblox Luau does have continue and goto is not recognized

or _, item in ipairs(ThingsToCheck) do
	if not Location:FindFirstChild("Store") then continue end
	if not Location.Store:FindFirstChild("Shelves") then continue end
	if not Location.Store.Shelves:FindFirstChild("Cans") then continue end
	if not Location.Store.Shelves.Cans:FindFirstChild("Beans") then continue end
	if not Location.Store.Shelves.Cans.Beans:FindFirstChild("Green") then continue end
	if not Location.Store.Shelves.Cans.Beans.Green:FindFirstChild("String") then continue end

	print("Found Green String Beans for", item)
end

ever heard of variables :open_mouth: aaaaaa

for _, item in ipairs(ThingsToCheck) do
	local store = Location:FindFirstChild("Store")
	if not store then continue end

	local shelves = store:FindFirstChild("Shelves")
	if not shelves then continue end

	local cans = shelves:FindFirstChild("Cans")
	if not cans then continue end

	local beans = cans:FindFirstChild("Beans")
	if not beans then continue end

	local green = beans:FindFirstChild("Green")
	if not green then continue end

	local str = green:FindFirstChild("String")
	if not str then continue end

	print("Found Green String Beans for", item)
end
1 Like

get rid of ipairs, it’s useless

function getNestedChild(ancestor: Instance, ...: string): Instance?
	local currentAnscestor: Instance = ancestor
	for i, name in {...} do
		local child = currentAnscestor:FindFirstChild(name):: Instance?
		if not child then
			return nil
		end
		
		currentAnscestor = child
	end
	
	return currentAnscestor
end

-- autocomplete doesnt register but you can do `x ::typeof(workspace.Location.Store.Sheveles.Beans)`
getNestedChild(workspace, "Location", "Store", "Shelves", "Beans")
1 Like

Here is a screenshot of the example, if it helps:

It never gets past Test_1 since that location does not have a folder named “String”.

go lurk in hiddendevs with this abomination of a code

1 Like

You got return in all those if statements. It should be continue to skip the iteration

This is actually pretty cool, I like this solution because it makes it nicer to check for any given item. You should mark this one as a solution OP :rofl:

for i = 1, #ThingsToCheck do
	local item = ThingsToCheck[i]

	local store = Location:FindFirstChild("Store")
	if not store then continue end

	local shelves = store:FindFirstChild("Shelves")
	if not shelves then continue end

	local cans = shelves:FindFirstChild("Cans")
	if not cans then continue end

	local beans = cans:FindFirstChild("Beans")
	if not beans then continue end

	local green = beans:FindFirstChild("Green")
	if not green then continue end

	local str = green:FindFirstChild("String")
	if not str then continue end

	print("Found Green String Beans for", item)
end

did i ever tell you to use for i loop? i said get rid of ipairs

I just prefer it like that, still should work, you’re not my boss :rofl:

how often do you start involving parents when losing an “argument”?

Just saying it because I noticed how horrible you’re attitude and lack of empathy is. Anyways, I have better things to do than argue with children on the internet, you obviously lack maturity and I don’t, so I’m just going to help us out and leave it here. I won’t be responding to your next message. (Even though you probably aren’t mature enough to stop this argument and not respond.)

4 Likes

This is the solution you are looking for, but all of these checks should not be necessary in the first place. Is the script on the client or server? Are these objects under workspace?

1 Like

I cannot figure this out, it sends a string that is not an actual location:

"Location", "Store", "Shelves", “Beans”

So, when the for loop tries to find it, the location doesn’t exist.

I need to confirm that things exists before executing code. I am not sure how this solution does that.

This is just an example.

I am not actually looking for Green String Beans in a store.

I want to understand how to find a nested item with a for loop without stopping the loop if the item does not exists for a particular player.