In pairs that does all at once?

Recently i’m trying to create an effect that makes base parts blink with transparency changing. I want all the parts called in the in pairs to blink all at the same time but the base parts seem to blink 1 at a time. Is there a in pairs of some kind that simulates all at the same time instead of individually?

For reference, here’s the code:

function changeTransparencyEffect()
	if CTE_Debounce == true then
		CTE_Debounce = false
		for _,v in pairs(script.Parent.Parent.colorme:GetChildren())do
			repeat
				v.Transparency = v.Transparency+0.2
				wait(0.1)
			until v.Transparency >= 1
			repeat
				v.Transparency = v.Transparency-0.2
				wait(0.1)
			until v.Transparency <= 0
		end
		CTE_Debounce = true
	end
end

If you want to yield inside the for loop then you’re going to need to coroutine wrap the inside and let it run on a psuedo-thread.

Unfamiliar with coroutines? Check this article:


Alternatively, you could possibly use tweens, these will be more customisable and in-time.

Using wait and not accounting for DeltaTime will mean that it will just get slower and slower

1 Like

To extend your post

By the way instead of using a repeat until loop here, use a numeric for loop

for i = 1, 0, -0.2 do
    v.Transparency = i
    wait(0.1)
end

PS the proper terminology for that loop is “generic for loop”, not “in pairs loop”

1 Like

I’ll be sure to change that. Ty

I’m unfamiliar with co routines so i’ll look into it, thanks for the suggestion

The simplest solution is just to swap your loops. Right now your inner loop tweens teens transparency and the outer loop goes over all parts.

If you swap that to make the outer loop tween transparency then the inner loop to iterate over the parts and apply it it’ll fix your issue.

Also you’re using the wrong function; pairs is for dictionaries and mixed tables, but what you want is ipairs which is for arrays. This isn’t directly related to the issue but you should fix.

Alternatively, the best solution was what someone already mentioned of using TweenService.

4 Likes

Thank you for your suggestion. I’ll be sure to add that once I get my hands on a computer. I did try looking into tween service but I thought it was only for animating objects.

function changeTransparencyEffect()
	if CTE_Debounce == true then
		CTE_Debounce = false
		for _,v in pairs(script.Parent.Parent.colorme:GetChildren()) do
                   
 spawn(function()game:GetService('TweenService'):Create(v,TweenInfo.new(1),{Transparency = 1}):Play()wait(1) game:GetService('TweenService'):Create(v,TweenInfo.new(1),{Transparency = 0}):Play()end)
		end
		CTE_Debounce = true
	end
end

Made to probably fit your liking

2 Likes

You should space out your code so that you have at most 1 statement on each line, so that it’s more obvious what your solution is doing at a glance.

Also I do believe TweenService already has a TweenInfo(?) property that automatically plays the tween backwards.

1 Like