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

Is there to make all of the parts within a group change transparency in sync?

Asked by
snarns 25
7 years ago

I'm trying to make a robotic glowing effect using transparency. It would be ideal if all of the parts within the group would gradually change their transparency in sync with one another without me having to add each and every part. Is there a way the script can call all of the children within a group in an easy and less messy way? Here's the script I've been using.

Fade = script.Parent
Fade2 = script.Parent.Parent.Light2
Fade3 = script.Parent.Parent.Light3
Fade4 = script.Parent.Parent.Light4

while true do
    Fade.Transparency = 0
    Fade2.Transparency = 0
    Fade3.Transparency = 0
    Fade4.Transparency = 0
    wait(1)
    Fade.Transparency = 0.2
    Fade2.Transparency = 0.2
    Fade3.Transparency = 0.2
    Fade4.Transparency = 0.2
    wait(0.2)
    Fade.Transparency = 0.4
    Fade2.Transparency = 0.4
    Fade3.Transparency = 0.4
    Fade4.Transparency = 0.4
    wait(0.2)
    Fade.Transparency = 0.6
    Fade2.Transparency = 0.6
    Fade3.Transparency = 0.6
    Fade4.Transparency = 0.6
    wait(0.2)
    Fade.Transparency = 0.8
    Fade2.Transparency = 0.8
    Fade3.Transparency = 0.8
    Fade4.Transparency = 0.8
    wait(0.4)
    Fade.Transparency = 0.6
    Fade2.Transparency = 0.6
    Fade3.Transparency = 0.6
    Fade4.Transparency = 0.6
    wait(0.2)
    Fade.Transparency = 0.4
    Fade2.Transparency = 0.4
    Fade3.Transparency = 0.4
    Fade4.Transparency = 0.4
    wait(0.2)
    Fade.Transparency = 0.2
    Fade2.Transparency = 0.2
    Fade3.Transparency = 0.2
    Fade4.Transparency = 0.2
    wait(0.2)
end

Doing this can get a little out of hand if it includes many parts.

2 answers

Log in to vote
2
Answered by 7 years ago
Edited 7 years ago

For Loops

You can use a GENERIC For loop (aka For each loop). It loops through a table using pairs() or ipairs(), here is the format when using the generic for loop:

for index, value in ipairs(t) do -- For each pair of index and value in the table...
    print(value) -- Example (Print the value)
end

Note that you can use _ instead of index if it is not necessary, but it is also good to keep the index variable just in case you need it in the future.

Now, you may wanna put all your parts in a Folder object in Workspace. Then, let's use :GetChildren() to get the list/table of parts you need to change with. Also, make a SERVER Script and put it in the folder you just created

local ListOfParts = script.Parent:GetChildren()

As you can see, I assigned the list to a variable named ListOfParts.

Now, use the generic For loop to loop through the list of parts.

local ListOfParts = script.Parent:GetChildren()

for i, part in ipairs(ListOfParts) do -- ipairs goes through lists like :GetChildren() arrays in order from the index 1. The variable "part" is the value.
    part.Transparency = 0.8
end

Since your wait times are different and you wanted to change the transparency to different values, let's try to make a NUMERIC For loop because the Transparency follows a pattern.

In case you don't know, numeric For loops look like this:

for i = 1, 3, 1 do -- The code is repeated 3 times, from 1 to 3 (incremented by 1).
    print(i, "thing(s)") -- Prints the current number (of times) in "i" and "thing(s)"
end

And in your case:

local ListOfParts = script.Parent:GetChildren()

for i = 0.2, 0.8, 0.2 do
    for _, part in ipairs(ListOfParts) do
        part.Transparency = i -- Continues to be increased by 0.2 until it reaches 0.8, starting from 0.2 itself!
    end
    wait(0.2)
end

We're not done yet! That's just part of the final code!

Now, let's make the whole script:

local ListOfParts = script.Parent:GetChildren()

while true do
    for i = 0.2, 0.8, 0.2 do
        for _, v in ipairs(ListOfParts) do
            if v:IsA("BasePart") then -- Check to see if this is a part, because the Script is counted as a child in the list as well.
                v.Transparency = i
            end
        end
        wait(0.2)
    end

    wait(0.2) -- The other 0.2 seconds are included in the loop. So that's 0.4 - 0.2 = 0.2

    for i = 0.8, 0, -0.2 do -- You can use numeric for loop for decrements as well! In that case, the value of "i" starts from 0.8 and keeps on decreasing until it reaches 0!
        for _, part in ipairs(ListOfParts) do
            for _, v in ipairs(ListOfParts) do
                if v:IsA("BasePart") then
                    v.Transparency = i
                end
            end
        end
        wait(0.2)
    end

    wait(0.8) -- The other 0.2 seconds are included in the loop as well. So that's 1.0 - 0.2 = 0.8
end

If you have any questions, please leave them in the comments! Thanks and I hope this will help! If this works, click "Accept Answer" so we can both get some reputation! :)

0
Wow! I must commend you for the work you've put into this. I wish I wasn't such a noob so I could understand it better, but your script works perfectly! Thank you so much! snarns 25 — 7y
0
It's ok. Learning takes time! :) starlebVerse 685 — 7y
Ad
Log in to vote
0
Answered by
cabbler 1942 Moderation Voter
7 years ago

Since there doesn't seem to a pattern here, you should store the parts in a table, then loop through the table in a function. You can supply the function parameters such as transparency and time.

I do reccomend you come up with a pattern to Lerp, so you do not have to this manually.

Fade = script.Parent
Fade2 = script.Parent.Parent.Light2
Fade3 = script.Parent.Parent.Light3
Fade4 = script.Parent.Parent.Light4

tab = {Fade,Fade2,Fade3,Fade4}

function fade(x,t)
    for _,f in pairs(tab) do
        f.Transparency=x
    end
    wait(t)
end

while true do
    fade(0,1)
    fade(0.2,0.2)
    fade(0.4,0.2)
    fade(0.6,0.2)
    fade(0.8,0.4)
    fade(0.6,0.2)
    fade(0.4,0.2)
    fade(0.2,0.2)
end
0
Well, there is kindof a pattern, but it seems unnatural if you know what I mean. cabbler 1942 — 7y
0
This is also a great answer! Thanks so much for the help! snarns 25 — 7y

Answer this question