Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why is roblox ignoring my if else statement?

Asked by 3 years ago

So i want the code to findfirstchild of an object, IF not then, do something else.

How ever when i write this:

if  workspace:FindFirstChild(v.Name).Crystal ~= nil then
                 print('hello found')
            else
                print('hello not found')
            end

It just says: "Crystal is not a valid member of Model "Workspace.masonsqui34gotband"

Why is roblox ignoring my else statement? Shouldn't this just ignore the if, and go straight to the "else" because it cant findfirstchild? why is is it erroring me

0
since crystal isn't a child of v.Name, it returns an error, use :FindFirstChild on the crystal as well to return nil and go directly to the else greatneil80 2647 — 3y

1 answer

Log in to vote
-1
Answered by
TGazza 1336 Moderation Voter
3 years ago

try:

-- assuming v is part of a for loop eg for k,v in pairs(table) do stuff end

local check = workspace:FindFirstChild(v.Name) -- make sure this is found and check it out below
if (check  ~= nil) and (check:findfirstchild("Crystal")~= nil) then -- if check is found and not nil then continue to find Crystal
    print('hello found')
else
    if (check == nil) then
        print(tostring(v.Name).." not found!")
        print('hello not found')
    else
        print('hello not found')
    end
end
Ad

Answer this question