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

How do i check if a child is in that parent?

Asked by
Neu_Dev 22
5 years ago
Edited 5 years ago

so i was trying to check if the weld constraint is in the parent or not apparently comparing it to nil does not work.

 if HitBox:FindFirstChild("WeldConstraint") == nil then
 print("Working")
end

2 answers

Log in to vote
0
Answered by
RBLXNogin 187
5 years ago

Hello there! As by your question it is great for me to assume two things. It appears as if you are trying to detect if an instance is inside an object, and detect if an instance is not inside an object. For this, first let me describe the problem with your previous code.

Your Script:

if HitBox:FindFirstChild("WeldConstraint") == nil then
 print("Working")
end

The problem with the code is that you are trying to detect equivilance. Basically your code is like saying if the FindFirstChild() function with a WeldConstraint parameter is nil then it will execute the code, which is your print command. This is your problem. FindFirstChild() detects if an object or instance of your choice is currently inside, and it will either return true or false (especially because your if statement). It appears as if you are trying to say if there is not a weld constraint (aka FALSE not nil), then execute the following code. Since you are a begginner there is a simple solution for you, which is the keyword not.

Here is a statement, from the lua reference manual:

The logical operators are and, or, and not. Like control structures, all logical operators consider false and nil as false and anything else as true. The operator and returns its first argument if it is false; otherwise, it returns its second argument. The operator or returns its first argument if it is not false; otherwise, it returns its second argument:

That being said, to detect if there is not a WeldConstraint in HitBox, your code should be:

if not HitBox:FindFirstChild("WeldConstraint") then
 print("Not Working")
end

This is simple :)

Now as for your second proposal, to detect if something is Inside of it, you do not need nil, or not. The reason is because that is find first child's primarily function

if HitBox:FindFirstChild("WeldConstraint") then
 print("Working")
end

This is your solution explained in detail. Hope it helped!

0
Well said! Thank you for this! i found out another solution but id rather change it to this for the purpose of optimizations. Neu_Dev 22 — 5y
0
@Neu_Dev your welcome! :) I love helping people on this website. Additionally, there are quite some other solutions such as "if HitBox.WeldConstraint then", but I'm glad my explainations helped you RBLXNogin 187 — 5y
Ad
Log in to vote
0
Answered by
Hypgnosis 186
5 years ago

FindFirstChild is meant to not error if the instance is nil, so you can remove that “== nil”. Other than that it should work if WeldConstraint is a child of HitBox.

if HitBox:FindFirstChild(“WeldConstraint”) then
     print(“Working”)
end

Answer this question