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

GetChildren function changes transparency for one child at a time?

Asked by 6 years ago

So... I made this script to dim lights that are all together in a folder. I want to dim all the lights at the same time (making them invisible, so a non-neon duplication of the lights becomes visible), but for some reason, the script dims one light at a time. Could somebody help me with this?

This is my script:

01for name, child in pairs(script.Parent.Parent.Preps.L:GetChildren()) do
02    if child:IsA("BasePart") then
03    local value = 0
04        for i = 1, 20 do
05            value = value + 0.05
06            child.Transparency = value
07            wait(0.05)
08        end
09    end
10end
0
Use coroutine or spawn User#19524 175 — 6y
0
its bc ur using a numeric for loop which yields User#23365 30 — 6y

2 answers

Log in to vote
0
Answered by 6 years ago

You will need to put the find children inside the for loop for transparency so it does them all at once:

1local value = 0
2for i = 1, 20 do
3    value = value + 0.05
4    for name, child in pairs(script.Parent.Parent.Preps.L:GetChildren()) do
5        if child:IsA("BasePart") then
6            child.Transparency = value
7        end
8    end
9end

This will loop through the transparency but inside of the transparency loop it will go through all the children then change transparency then repeat until its done.

0
This won't work without something inside the outer for loop that yields, ideally a Heartbeat:Wait(), otherwise you only see the end result. EmilyBendsSpace 1025 — 6y
0
Additionally, move the GetChildren() call outside both loops. It's wasteful to call it 20 times when it's presumably returning the same list of children each time. EmilyBendsSpace 1025 — 6y
0
For me, when I add a wait() in the loop, it works just like I needed it. Thank you so much! Dylan011444 59 — 6y
Ad
Log in to vote
0
Answered by 6 years ago

you can use coroutine.wrap(function() to dim out all the lights without yielding. a numeric for loop yields so it stops the script until it breaks or finishes. Just a reminder but you dont need to use a variable for the part's transparency to set to, just use the value.

01for name, child in pairs(script.Parent.Parent.Preps.L:GetChildren()) do
02    if child:IsA("BasePart") then
03        local dimlights = coroutine.wrap(function()
04            for i = 0,1,0.05 do
05                child.Transparency = i
06                wait(0.05)
07            end
08        end)
09 
10        dimlights()
11    end
12end

Answer this question