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:

for name, child in pairs(script.Parent.Parent.Preps.L:GetChildren()) do
    if child:IsA("BasePart") then
    local value = 0
        for i = 1, 20 do
            value = value + 0.05
            child.Transparency = value
            wait(0.05)
        end
    end
end
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:

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

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.

for name, child in pairs(script.Parent.Parent.Preps.L:GetChildren()) do
    if child:IsA("BasePart") then
        local dimlights = coroutine.wrap(function()
            for i = 0,1,0.05 do
                child.Transparency = i
                wait(0.05)
            end
        end)

        dimlights()
    end
end

Answer this question