How to avoid nesting code

You send a string when you try to access / index a property or a child:
obj.ChildName is synonymous to obj["ChildName"]


Could you elaborate?

like someone else mentioned just do FindFirstChild(ā€œItemNameā€, true) and it’ll do a recursive search until it finds that item name. or it’ll return nil if it can’t find it.

I need to confirm that the path exists before trying to update a value at the end of the path.

Example.

game.Workspace.Test_1.Store.Shelves.Cans.Beans.Green.String.Count.Value = 10 would error.

That is because Test_1 does not have a ā€œStringā€ folder.

I feel like you’re overthinking this. The solution is already one of the post in this thread. Just test them all see which works best for you.

local item = workspace:FindFirstChild(ā€œItemNameā€, true)

if item then
    item.Count.Value = 10
end

This should achieve what you are looking for. item will be nil if not found.

I have not seen a solution posted yet.

But, I do thank evey one for looking into it.

I need this exact pathway to exist before executing code:

game.Workspace.Test_1.Store.Shelves.Cans.Beans.Green.String, but it may or may not exists.

Why not have the item be what it is? CannedGreenBeans? If it’s for a store you can put all the items in a single folder and have an attribute or something with what shelf it’s linked to if you needed it.

Why over complicate it with having it by different types colors etc?

The problem with this idea is that there may be several things with the name ā€œStringā€ that may or may not be ā€œGreenā€.

So, I need the entire pathway confirmed:

game.Workspace.Test_1.Store.Shelves.Cans.Beans.Green.String

But, not everyone will have ā€œBeansā€, ā€œGreenā€, ā€œStringā€.

But there may be many different ā€œBeansā€, many different: ā€œGreenā€ and even many ā€œStringā€.

So that is why the specific pathway need to be confirmed, not just the word ā€œString"

Like I said, this is just an example.

I am not looking for Green String Beans.

the getNestedChild() function does exactly that. For each string you pass, it checks to see if a child with that name exists, and returns the child at the end. It is your solution.

1 Like

I gotchu

local success, my_path = pcall(function() return Location.Store.Shelves.Cans.Beans.Green.String end)
if not (success and my_path) then continue end
1 Like

Perhaps this isn’t working out for you or any of us. Would you be able to give more insight to what your actual use case is and we could possibly give better information on how to get the result your looking for?

Using a pcall is completely unnecessary. This is not the way to go.

I saw that solution, but it did not work.

I cannot pass the path as a value without first checking that it exists or it will error.

So the order would not be the same as others?

player1: A.B.C
player2: B.C.A
player3: C.B.A

Makes sense. Then I recommend this module: Tree | RbxUtil.

The path would always be the same, but may be of different lengths.

Like in the example, each Test_# has a different depth of folders.

The point of the pcall is that if it does not exist it will error. Additionally, I checked that String is not nil as well. What are you saying?

try this, maybe it needs explicit ipairs. I just tried it and it definitely works.


function getNestedChild(ancestor: Instance, ...: string): Instance?

	local currentAnscestor: Instance = ancestor

	for i, name in ipairs({...}) do

		local child = currentAnscestor:FindFirstChild(name) :: Instance?

		if not child then return nil end
		
		currentAnscestor = child
	end
	
	return currentAnscestor
end

i used this structure:
image

and ran this code:


-- returns the value folder
local instance = getNestedChild(DataModel, "EquippedItems", "key", "TagsArray", "value")

-- returns nil
local nonExistent = getNestedChild(DataModel, "EquippedItems", "DoesntExist", "AlsoDoesntExist")

1 Like

This is a recurring problem.

I run into the need to nest lines of code all the time when using for loops because the path needs to be confirmed before executing code.

That is why I put together a generic example to explain the problem.