"if not ..." not working correctly

I have this if statement

for i,v in pairs(TestFolder:GetDescendants()) do
	if not v:FindFirstChild("TestScript") then
		print("TestScript is Missing")
	end
end

but it is printing the

print("TestScriptis Missing")

even though the “TestScript” is there.

Can you show us your explorer?

I think it indexes all the other objects, it finds nothing in that and it prints it. The other and only case is where the TestScript is located, where it doesn’t print at all.

1 Like

I’m not sure but it may be because if not is only for boolean values (true false) but in this case you are checking if something exists. This wasn’t well explained but it might be the problem.

image

It would be helpful if you show us your explorer

i have a other script where it does basically the same and it works

Your v would be the local script, it would search to find TestScript in your localscript and there is no TestScript inside that’s why it prints that there is nothing called TestScripr

2 Likes

In the children of the localscript*

Actually, let’s try diagnose this. Print v within the for loop, because I have no idea what it bumped into. It might have been something added into the folder before it?

a other script checks for this script

Where is your for loop located in? Is it a local script or server script?

The for loop is in a LocalScript

And where is TestFolder located in?

The TestFolder is located in a ScreenGui

for i,v in pairs(TestFolder:GetDescendants()) do
	if not v:FindFirstChild("TestScript") then
		print("TestScript is Missing")
	end
end

While looping through the TestFolder’s descendants, v is going to be the descendant - because TestScript is parented to TestFolder, you’re checking if TestScript has a child named TestScript, which is why it’s not printing anything. I’m fairly sure if you printed v it’d return TestScript

1 Like

how about you use :WaitForChild()

still the same problem and im checking if the TestFolder has a child named TestScript and if the TestFolder has no child named TestScript then it should print the … is missing

for i,v in pairs(TestFolder:GetChidren()) do
	if not v.Name == "TestScript" then
		print("TestScript is Missing")
	end
end

If that’s what you want to do then can’t you just do thsi instead?

if not TestFolder:FindFirstChild("TestScript") then
    print("TestScript is missing")
end
2 Likes