Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

GetChildren function changes transparency for one child at a time?

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

2 comments

0

Use coroutine or spawn

0

its bc ur using a numeric for loop which yields

2 answers

✓ Accepted answer

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.

3 comments

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.

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.

0

For me, when I add a wait() in the loop, it works just like I needed it. Thank you so much!

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

This discussion is preserved as it was. New answers, comments, and votes are closed.