Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why is roblox ignoring my if else statement?

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

1 comment

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

1 answer

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

This discussion is preserved as it was. New answers, comments, and votes are closed.