Change a Model's Transparency via Script

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

14 Likes

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.

EDIT: This may help you: https://developer.roblox.com/en-us/api-reference/function/Instance/GetChildren

5 Likes

This should do the trick

function transparency(model, transparency)
   for i, part in pairs (model:GetChildren()) do
      part.Transparency = transparency
   end
end

(haven’t tested it though)

btw you gotta call it with this transparency(model,1)

3 Likes

The reason I thought this was relevant to post was that the above function wouldn’t work if you insert anything other than a parT.

 function transp(model, transparency)
        for i,v in pairs(model:GetChildren()) do
            if v:IsA("BasePart") then
                v.Transparency = transparency
            end
        end
    end
2 Likes

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).

If you have any questions just let me know!

~BuDeep

Edit: Changed “BasePart” to “Part”

17 Likes
for i,v in pairs(workspace.Colors:GetChildren()) do
   v.Transparency = 1
end
19 Likes

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.

7 Likes

Is this better performance wise? Why is it better practice, and what do you mean by idiomatic?

2 Likes

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.

See the above response, as it covers that detail:

3 Likes

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.

2 Likes