How to avoid nesting code

Use another for loop to confirm if the path exists.

local Location = workspace.Location


local success, my_path = pcall(function() return Location.Store.Shelves.Cans.Beans.Green.String end)
if not (success and my_path) then
	print("DNE")
else
	print("exists", my_path)
end

image
image
I don’t get it, this code works why doesn’t the solution work for you?

1 Like

this is also a very good solution. It does exactly what the OP wants with no extra bloat. I think it’s better than the other solution.

if you want it to error without stopping the loop, then try task.spawn

-- inside for loop:
    task.spawn(function()
     -- for loop code
    end)

This way you can still see errors and the code outside the task.spawn will continue running.

Thank you.

Your post help me to understand the Solution that was posted.

I would select both post as the solution if I could.

Did some reading on pcall.

Looks like it was created specifically for my needs.

In Roblox Lua scripting, pcall() is used to execute a function in a “protected mode,” meaning it catches any errors that might occur during the function’s execution and returns a status code and any error message. This prevents errors from halting the script’s execution and allows for error handling.

Your suggestion, with some adjustments does the trick more simply:

for _, TestFolder in pairs(game.Workspace.Location:GetChildren()) do
	if TestFolder:IsA("Folder") then
		local Success, Error = pcall(function() return game.Workspace.Location[TestFolder.Name].Store.Shelves.Cans.Beans.Green.String end)
		if not Success then warn("Path Failed:", Error) else

			-- continue with script

			print("Path is valid.")

		end

	end
end

Changing Solution to yours.