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?
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.
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
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
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:
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")
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.