Thought I found a way to find an object (if it exists) with the same attribute with another without a loop

Correct me if I’m wrong, but I think I found a way to find a specific object with specifications from the same local. It occurred to me that maybe putting parentheses could be possible, since the system gets confused and points out the problem if you don’t put parentheses, but apparently with the parentheses it doesn’t mark the error.

local asd = (Backpack:FindFirstChild(Slot[2].Name) and Backpack[Slot[2].Name]:GetAttribute("Type_Tool") == Slot[2]:GetAttribute("Type_Tool"))
			
if asd then 
	asd.Stack.Value = asd.Stack.Value +1
else
	if not ToolsFolder.FoodIngredients:FindFirstChild(Slot[2].Name) then return end
	local Found_tool = ToolsFolder.FoodIngredients[Slot[2].Name]:Clone()	
	Found_tool:WaitForChild("Stack").Value = 1
	Found_tool.Parent = Backpack
end

The FindFirstChild is used to determine if it exists, and if it exists it does the rest, which is to find if the other object has the same attribute as the other. I do not know if I am correct, or if it works and in the future it will give me possible errors.

Of course. Ternary operations are meant for scenarios like this when the result is a boolean.

I finally discovered this, now I will be able to optimize the scripts more. Before I used loops to find them and it worked, but this is better for optimization.

oh no, i thought i would be looking for exactly the object. But it gives an error, and from print it gives me “true”

apparently you don’t get the tool, what you get is a boolean of false

Why not just use the first method (FindFirstChild)? I think it just over complicates the readability of your code.

You mean to search for the tool with FindFirstChild and then with a condition to search for the chosen attribute?

I guess you could do that, yeah…

Well, there will be many tools and all the tools will be saved. I am wanting to optimize as much as possible (both with tools, models and scripts), with models in blender, with shortening scripts, with the tools it will be differentiated with the attributes (an example is own ax and work ax, I use the attributes to differentiate them and thus not having free tools).

ah, by that I mean it will pick a random tool if they have the same names (differentiate with attributes)