I’m trying to detect if player is holding a Tool in either backpack or character but FindFirstChildWhichIsA is returning nil if it doesn’t, unlike FindFirstChild which returns false.
using pcall() will always return false which I don’t want, because in this case I don’t want the player to hold a duplicate item that has the same attribute.
if Player.Backpack:FindFirstChildWhichIsA("Tool"):FindFirstChild("StateGive"):GetAttribute("IsStone") == true then
return true
else
if Player.Character:FindFirstChildWhichIsA("Tool"):FindFirstChild("StateGive"):GetAttribute("IsStone") == true then
return true
end
end
Are you confusing it for something? FindFirstChild will return nil if it doesn’t find what it is looking for. They should both return nil if the instance doesn’t exist; that is the expected behavior.
I cannot stress enough how bad of a practice this is. You should never be using FindFirst_ methods like this. In the case that one of them return nil, the entire code will error.
Consider reading this thread below, specifically the second bad habit listed which is what you are doing.
I was just addressing the problem you are having. Try the following:
local tool = Player.Backpack:FindFirstChildWhichIsA('Tool')
if tool then
local stateGive = tool:FindFirstChild('StateGive')
if stateGive and stateGive:GetAttribute('IsStone') then return true end
end
local char = Player.Character
if char then
local tool = char:FindFirstChildWhichIsA('Tool')
if tool then
local stateGive = tool:FindFirstChild('StateGive')
if stateGive and stateGive:GetAttribute('IsStone') then return true end
end
end
Still gives me duplicate tool that has the same attribute.
Edit: Fixed it on my own.
local __TOOL = Player.Backpack:FindFirstChildWhichIsA("Tool")
if __TOOL then
local __ISSTONE = __TOOL:FindFirstChild("StateGive")
if __ISSTONE and __TOOL:GetAttribute("IsStone") == true then return true end
end
local __CHARACTER = Player.Character
if __CHARACTER then
local __TOOL = __CHARACTER:FindFirstChildWhichIsA("Tool")
if __TOOL then
local __STONE = __TOOL:FindFirstChild("StateGive")
if __STONE and __TOOL:GetAttribute("IsStone") == true then return true end
end
end