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