Hi, so I’m trying to make a script that makes a Model transparent, but I’m not sure how to do it, I thought it could be something to do with Children, but I just have no idea.
This is what my model looks like, I want to change the transparency of the entire model, how would I do this.
My Model with all the Parts
You can use GetChildren/GetDescendants on the model to get all the parts, then use “v” to change the transparency to however you want inside the for loop.
A method that’s really useful for grabbing multiple parts is one called :GetChildren().
Basically what this does is it creates an array of all the items inside the referenced area. For example:
workspace.Model:GetChildren()
This would grab all the items inside of the model, and report them back as a table. However to access the table we have to reference it. This is where things like Loops come in handy. The one I most commonly use for this scenario would be the for do loop. An example below:
for _,obj in pairs (workspace.Model:GetChildren()) do
--Do whatever with obj here
end
The above would take the direct descendants of the model and reference them as obj. The loop then goes through each item, saved as obj, and allows you to make changes to them. So in order to change the transparency all you would have to do is:
for _,obj in pairs (workspace.Model:GetChildren()) do
obj.Transparency = 1
end
Wallah! Your script will now change all the items inside of your script to have a transparency of 1. However let’s say you have an item inside of the model that is not a BasePart. If the loop goes through as shown above, it will throw an error as it cannot set the transparency of a IntValue or PointLight to 0. In order to prevent this you need to make your loop a bit more defined in what it’s looking for. This is where a if then statement comes in use:
for _,obj in pairs (workspace.Model:GetChildren()) do
if obj.ClassName == "Part" then
obj.Transparency = 1
end
end
Perfect! The above will now run error free, as it will only perform the action if the items ClassName is BasePart (which means said item is a part).
The answers here work but as a minor note it’s generally adviced, idiomatic and better practice to use ipairs instead of pairs when dealing with arrays.
You should be checking the class of the objects you’re iterating through and making sure they’re BaseParts. Any instance without a Transparency property that is a child of the model will cause this script to error.
It’s better for performance, yes. ipairs was designed specifically for arrays, as opposed to pairs for dictionaries.
It’s idiomatic in that that’s its purpose and when you see it you know “ah I’m iterating an array” as opposed to the ambiguity of pairs.