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

How to get every part in a model?

Asked by
zomspi 541 Moderation Voter
4 years ago

How might I get all the parts in a model? I have a bunch of scripts etc inside of a model and I am trying to get all the parts inside the model so I can alter the part's properties etc. Is there a way to get all the parts of a model?

3 answers

Log in to vote
1
Answered by 4 years ago

You can get all the parts inside a model or really any instance by using the :GetDescendants() function.

It would look something like this

local model = game.Workspace.Model --//If there is a model in workspace

for i, v in pairs(model:GetDescendants()) do
    print(v.Name) --//Will print out the name of every descendant of the model
end

If you wanted to check just for parts/baseparts, you could do this

local model = game.Workspace.Model --//If there is a model in workspace

for i, v in pairs(model:GetDescendants()) do
    if (v:IsA("BasePart")) then
        --//The part we are currently on inside the loop is a part/basepart
    end
end

More info on GetDescendants here.

0
ahh ok thx zomspi 541 — 4y
Ad
Log in to vote
0
Answered by
zomspi 541 Moderation Voter
4 years ago

Thanks, this is my script with yours but it is not working?


z = script.Parent while true do wait(1) if z.Zombie.Health == 0 then local charChildren = z:GetChildren() --"char:GetChildren()" is a table containing all children in Character-- for i,v in pairs(charChildren) do --"for loop" to run through objects in table-- if v:IsA"Part" then -- "If Then Statement" to check if current Object is a Part-- print(v.Name .. " -- IS A PART") -- printing "Object is a Part" if Object passes the Check end -- closes the "if then statement" end -- closes the "for loop" end end
0
It should work correctly when the Zombie is dead ryan32t 306 — 4y
Log in to vote
-1
Answered by
ryan32t 306 Moderation Voter
4 years ago

Use ":GetChildren()" on the model to get a table of all the children in the model and use an "If Then Statement" to check for parts; Example:

local plr = game.Players.LocalPlayer
local char = plr.Character
local charChildren = char:GetChildren()
--"char:GetChildren()" is a table containing all children in Character--
for i,v in pairs(charChildren) do
--"for loop" to run through objects in table--
if v:IsA"Part" then
-- "If Then Statement" to check if current Object is a Part--
print(v.Name .. " -- IS A PART")
-- printing "Object is a Part" if Object passes the Check
end
-- closes the "if then statement"
end
-- closes the "for loop"
0
I tried changing it to suit me but it didn't work, my script is below zomspi 541 — 4y

Answer this question