Am I going insane or is this if statement not working?

Fairly simple stuff, when the character is added, a server script runs and prints all the children of the character, excluding the HumanoidRootPart and the Humanoid via an if statement.

local function CharAdded(Character)
	for _, v in pairs(Character:GetChildren()) do
		if v:IsA("Part") or v:IsA("BasePart") or v:IsA("MeshPart") or v.Name ~= "HumanoidRootPart" then
			local part = v
			print(part)
		end
	end
end

BUT WHY IS IT STILL PRINTING THE HUMANOID
image

And no, I don’t want a band-aid fix like specifically excluding the humanoid in the if statement, I want an explanation as to why the humanoid isn’t being filtered out by the if statement.

1 Like

Make it check if it’s a Humanoid to avoid this then.

I want to say Humanoid is considered a BasePart, and that is what is causing it.

local function CharAdded(Character)
	for _, v in pairs(Character:GetChildren()) do
		if v.Name ~= 'Humanoid' and (v:IsA("Part") or v:IsA("BasePart") or v:IsA("MeshPart") or v.Name ~= "HumanoidRootPart") then
			local part = v
			print(part)
		end
	end
end

Please read the last paragraph of my original post.

I replied before you edited the post to say that, so I edited mine.

But this is clearly not the case as I’ve checked what the class of the Humanoid is.

local function CharAdded(Character)
	for _, v in pairs(Character:GetChildren()) do
		print(v, " ", v.ClassName)
    end
end

image

image
Never mind, it is not considered a base part.

Also, some meshparts are considered “BaseParts” aswell for some reason. It’s confusing.

Humanoid is considered a sibling of the BasePart, but I am unsure if that is the actual issue or not.

Because it checks the final condition and sees the humanoid.Name is not equal to "HumanoidRootPart" and thus runs the code under the if statement.

Fairly simple.

Didn’t catch this! Try this instead then:

local function CharAdded(Character)
	for _, v in pairs(Character:GetChildren()) do
		if (v:IsA("Part") or v:IsA("BasePart") or v:IsA("MeshPart")) and v.Name ~= "HumanoidRootPart" then
			local part = v
			print(part)
		end
	end
end

The Humanoid is not a BasePart since this does not inherit properties from the class. The real reason is because each condition is chained or operators. The way it works is it goes through every condition and if it is true, will yield true as the result, else it returns the next one. The condition checks for if it is a base part or similar does fail, but the one check it passes is the name check to ensure it is not named “HumanoidRootPart”.

Your desired result is to have an instance that is both a base part “and” is not named “HumanoidRootPart”, right? In this case, you need the and operator:

if (v:IsA("BasePart") and v.Name ~= "HumanoidRootPart") then
    -- do stuff
end

P.S: Parts and mesh parts are base parts since the first two inherit members from the BasePart class

1 Like
if (v:IsA("Part") or v:IsA("BasePart") or v:IsA("MeshPart")) and v.Name ~= "HumanoidRootPart" then
	local part = v
	print(part)
end

@6Square4 ye mb I didn’t read ur code correctly, so OP, if your reading this, please try @6Square4’s solution.

1 Like


It did indeed fix it.

The reason why it’s not working is because you used “OR” instead of “AND” for your if statement, basically the last condition of your if statement

if v.Name ~= "HumanoidRootPart"  then

And Humanoid is not a HumanoidRootPart therefore the if statement returns true and the conditions is met.

You’re good. You caught the one thing I didn’t even notice and I used a band-aid solution (bad practice), learning experience for both of us!

Have a blessed day all!

1 Like