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

How to change the property of all parts in a model?

Asked by 9 years ago

How would I change the property of all parts in a model? Is there a way?

Thanks!

2 answers

Log in to vote
3
Answered by
Muoshuu 580 Moderation Voter
9 years ago

Apple's code is nice but here is a more flexible one (Recursive too!)

function SetProperties(Item,Class,PropName,Value,Recursive)
    if Item and PropName and Class and Value~=nil then
        for _,v in pairs(Item:GetChildren()) do
            if v.ClassName==Class then
                if v[PropName] then
                    pcall(function() v[PropName]=Value end)
                end
                if Recursive then
                    SetProperties(v,PropName,Value,Recursive)
                end
            end
        end
    end
end
SetProperties(PathToModel,"Part","Name","Name of Part",true)
Ad
Log in to vote
2
Answered by
apple 10
9 years ago

Assume the model is named "Model" and is in workspace, and the property you're looking for is "Property", and the value you want is "Value". Also assume the type of object is a Part

for _,v in pairs(game.Workspace.Model:GetChildren()) do
    if v:IsA("Part") then
        v.Property = Value
    end
end

First line loops through the model making v equal to the child for each child, next checks if the child is the correct type of object, then everything before the end is in the loop that messes with each individual child.

This only works for direct children of the model, by the way, so if there's a model inside the model it won't work.

The second line can also be replaced with any other sort of check, but needs to make sure it only edits the parts you want it to.

0
You'd also want to check if the item is actually a part and not, for instance, a script, or a dog Muoshuu 580 — 9y
0
yes, I forgot about that, thanks~ i'll add it apple 10 — 9y

Answer this question