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

:GetChildren() loop wont work correctly?

Asked by 5 years ago

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

0
only anchor HumanoidRootPart yHasteeD 1819 — 5y

2 answers

Log in to vote
0
Answered by
Rheines 661 Moderation Voter
5 years ago
Edited 5 years ago

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
0
Thanks it works now EnderPowerHD 98 — 5y
0
You did not explain why the OP's code failed. User#24403 69 — 5y
Ad
Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

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

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 theking48989987 2147 — 5y

Answer this question