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

How can I make parts in all models transparent? (by script)

Asked by 6 years ago

I have a script that makes semi-transparent Parts only in workspace ... But if I put Parts into the model ... they are no longer transparent.

How can I make all these parts in each model transparent?

Script:

for _, child in ipairs(game.Workspace:GetChildren()) do
    if child.ClassName == "Part" then
        child.Transparency = 0.90
wait(0.1)
    end
end

Can someone help me with this script?

2 answers

Log in to vote
0
Answered by
Rare_tendo 3000 Moderation Voter Community Moderator
6 years ago

Can use this script I've provided, with an explanation provided below.

function transparency(obj) --Creeating our function
       for _,v in pairs(obj:GetChildren()) do --generic for loop for iterating through all of "obj"'s children
              if v:IsA('Part') then --Check to see if the current value is a part instance
                     v.Transparency = 0.9 --Change the part's transparency
              end
              if v:IsA('Model') then --Check to see if the current value is a model instance
                     transparency(v) --Recursion occurs here. Here, we make the function call itself to change the transparency of other parts in the model
              end
       end
end

transparency(workspace) --This line will go through all objects in workspace, and if a part is found, its transparency will change, but if a model has been found, it will change the transparency of all parts in the model.
Ad
Log in to vote
0
Answered by 6 years ago
Edited 6 years ago
for _, child in ipairs(script.Parent:GetChildren()) do
    if child.ClassName == "Part" then
        child.Transparency = 0.90
wait(0.1)
    end
end

Nice and easy fix on the first line, when you do game.Workspace:GetChildren() it's going through workspace, you need to change it to the model, so if the script is inside the model you want to make transparent then you change game.Workspace to script.Parent, as I've done for you above.

Also, I don't know if this is here on purpose or if you think you need it, but the wait(0.1) isn't necessary. I believe you got this from the while true do function, but that's because that one runs as fast as possible so you need a wait, your for loop doesn't really need it since I doubt you have hundreds of thousands of parts in your model.

Answer this question