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

:GetChildren() loop wont work correctly?

i want to Anchor all parts/MeshParts or an R15 player but when i try this

local plaan = char:GetChildren()
for i=1, #plaan do
if plaan[i].ClassName=="MeshPart" or "Part" then
	plaan[i].Anchored=true
end
end

it still tries to Anchor Humanoid

1 comment

0

only anchor HumanoidRootPart

2 answers

✓ Accepted answer

For this, I particularly recommend using pairs to iterate through a table and :IsA() to find parts and anchor them in the rig.

local plaan = char:GetChildren()

--Since we are not using the index, we can set it to _.
--v here is the objects in the table.
for _,v in pairs(plaan) do
	--"BasePart" automatically detects Parts and MeshParts.
	if v:IsA("BasePart")  then
		v.Anchored = true
	end
end

2 comments

0

Thanks it works now

0

You did not explain why the OP's code failed.

Logic operations in programming


Unlike in everyday, where common sense acts as a filler for some shortfalls, no such thing exists in programming. With that said, the problem here is line 3 for you,

if plaan[i].ClassName=="MeshPart" or "Part" then

it is generally implied in everyday english that the meaning of it is:

if plaan[i].ClassName=="MeshPart" or plaan[i].ClassName=="Part" then

but, no implications like that occur with the complier, which is exceptionally picky,instead of that is checks if plaan[i].ClassName == "MeshPart" is a truthy value(not false or nil) and checks if "Part" is a truthy value, instead of checking if plaan[i].ClassName is part.

Hopefully this helped solve your problem

1 comment

0

i would also like to point out that you can check if plaan[i]:IsA("BasePart"), which i think encompasses both meshparts and normal parts

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