How to make a condition fire if there is no tool with such an attribute

Well, I’m trying to ensure that if there is no tool with the “IngredientJob” attribute, the condition is met. Trying to do this I get the following error: attempt to index nil with 'GetAttribute'

elseif not character:FindFirstChild(SelectedObject):GetAttribute("Type_Tool") == "IngredientJob" then
	print("True")
end
1 Like
elseif not character:FindFirstChild(SelectedObject,true):GetAttribute("Type_Tool") == "IngredientJob" then
	print("True")
end

Maybe Try this

1 Like

Also “SelectedObject” Should be a string

2 Likes

“SelectedObject” is the name of a tool searched with GetChildren. Wait a minute, I’m going to try this:

1 Like

Well, it keeps giving me the error. I have no idea what to do

1 Like

Are you sure the attribute is named that way?
if yes then send a picture

2 Likes

Yes. Maybe, if you don’t have any tool and you activate the function, when trying to find the tool, it gives the error because there is no tool that can collect the attribute

elseif character:FindFirstChild(SelectedObject,true):GetAttribute("Type_Tool") ~= "IngredientJob" then
	print("True")
end

what about you try this then?

It still gives the same error, maybe I should mess with GetChildren

yea I think you should check something there

Well, at the end I put:

elseif character:FindFirstChild(SelectedObject):GetAttribute("Type_Tool") == "IngredientJob" then
--two condition
else
	print("True")
end

and it worked

local tool = character:FindFirstChild(SelectedObject)
if tool then
	local attVal = tool:GetAttribute("Type_Tool")
	if attVal == "IngredientJob" then
		--stuff here
	end
else
	--stuff here
end

You should be doing this in parts to mitigate the risk of errors. Your error was occurring because you were calling the instance method :GetAttribute() through a nil value (when the tool wasn’t found in the player’s character).