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?
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
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
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"