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

Why is this not working and only fading one at a time?

This script is only fading each brick one at a time and not all together. Any help will be welcomed.

local Epic=script.Parent.ClickDetector
function onClick()
for i,v in pairs(game.Workspace.Model:GetChildren()) do
for i = 0, 1, 0.1 do
v.Transparency = 1 - i
wait(0.1)
end
end



end
Epic.MouseClick:connect(onClick)

2 comments

0

This is because for loops only work on one task at a time, it won't do it all at once.

0

This is because for loops only work on one task at a time, it won't do it all at once.

1 answer

✓ Accepted answer

For this, I recommend the use of coroutines

Coroutines create a separate thread where you can run your loops without disturbing the flow of your main script.

There are 2 key functions that you require: coroutine.resume(c) - c being a created coroutine coroutine.create(f) - f being a function

You must create a coroutine before using it (by calling resume()) There are a couple of ways of making a coroutine, I will show you one.

coroutine.resume(coroutine.create(function() 
 -- Your either looped script or for script
end))

So, for you it should go like this:

local Epic=script.Parent.ClickDetector
function onClick()
	for i,v in pairs(game.Workspace.Model:GetChildren()) do
		coroutine.resume(coroutine.create(function()
			for i = 0, 1, 0.1 do
				v.Transparency = 1 - i
				wait(0.1)
			end
		end))
	end
end
Epic.MouseClick:connect(onClick)

Hopefully that should work for you now

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