How to properly use FindFirstChild in an "If Statment"

When I’m using FindFirstChild I usually write it as the example below. It usually works…

If Model:FindFirstChild("Handle") == true then
-- function
end

However recently I came across a problem that even though the object would exist the script would ignore the statement unless I write it as the example below. Writing it in this manner doesn’t give me any errors, so it got me wondering if I’m writing it wrongfully

If Model:FindFirstChild("Handle") then
-- function
end

instances are truthy, meaning that when comparing, they will return true.

this does not mean that the instance is equal to true though.

If Model:FindFirstChild("Handle") == true then
    -- this is asking if the handle itself equals to the boolean value 'true'. this fact is false, so this wouldnt work
end
If Model:FindFirstChild("Handle") then
    -- this is asking if the handle is true or false, however you cannot compare without a boolean, so the script converts the handle to true, so that it can compare between the handle and true.
    -- this is the same reason why when comparing nil, the script treats nil like the boolean value of false.
end

im sorry if that explanation was kinda confusing, im not very good at explaining things.
if you want more information on this, google up truthy and falsey values.

1 Like

the capitalization is off but maybe the first one is wrong because you don’t need == true but also, maybe it thought true == true

I understand what you’re saying, this was very helpful and thank you for helping me understand!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.