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 10 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
10 years ago

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

01function SetProperties(Item,Class,PropName,Value,Recursive)
02    if Item and PropName and Class and Value~=nil then
03        for _,v in pairs(Item:GetChildren()) do
04            if v.ClassName==Class then
05                if v[PropName] then
06                    pcall(function() v[PropName]=Value end)
07                end
08                if Recursive then
09                    SetProperties(v,PropName,Value,Recursive)
10                end
11            end
12        end
13    end
14end
15SetProperties(PathToModel,"Part","Name","Name of Part",true)
Ad
Log in to vote
2
Answered by
apple 10
10 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

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

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 — 10y
0
yes, I forgot about that, thanks~ i'll add it apple 10 — 10y

Answer this question