How do I stack two tweens together in an In Pairs loop without them breaking each other?

I’m almost done with this script and I’ve been running into the same issue over and over again where I can only have one tween function properly within an in pair loops. If I add another tween, it manages to mess up the other. Here’s the code :

UIS.InputBegan:Connect(function(input, gameProcessed)
	if gameProcessed then return end
	if input.KeyCode == zflashkeybind then
		anim:AdjustSpeed(0.6)
		flashevent:FireServer()
		hrp.Anchored = true
		local Animation = TweenService:Create(hrp, info1, {["CFrame"]= hrp.CFrame + hrp.CFrame.LookVector * -35}):Play()
		hrp.Anchored = false
		for i, v in pairs(player.Character:GetDescendants()) do
			if v:IsA("BasePart") or v:IsA("MeshPart") or v:IsA("Decal") then
				local fadeOut = TweenService:Create(v, info2, {["Transparency"] = 1})
				fadeOut:Play()
				fadeOut.completed:Wait()
				local fadeIn = TweenService:Create(v, info2, {["Transparency"] = 0})
				fadeOut:Play()
			end
		end
	end
end)

The goal is for the first tween (fadeOut) to turn the player invisible all at once, while the second tween (fadeIn) makes them visible again only after fadeOut is completed. I tested each tween and made sure they function by themselves, so they are not the problems. The problem seems to be using the In Pairs loop, it seems to not cooperate and makes it so that fadeOut turns the player transparent one component at a time (ex. their torso fades, then their head, then their arm, etc etc). Even afterwards, fadeIn doesn’t function - the player just remains invisible. Does anyone have an alternative solution or am I overlooking something?

1 Like
UIS.InputBegan:Connect(function(input, gameProcessed)
	if gameProcessed then return end
	if input.KeyCode == zflashkeybind then
		anim:AdjustSpeed(0.6)
		flashevent:FireServer()
		hrp.Anchored = true
		local Animation = TweenService:Create(hrp, info1, {["CFrame"]= hrp.CFrame + hrp.CFrame.LookVector * -35}):Play()
		hrp.Anchored = false
		for i, v in pairs(player.Character:GetDescendants()) do
			if v:IsA("BasePart") or v:IsA("MeshPart") or v:IsA("Decal") then
				spawn(function()
					local fadeOut = TweenService:Create(v, info2, {["Transparency"] = 1})
					fadeOut:Play()
					fadeOut.completed:Wait()
					local fadeIn = TweenService:Create(v, info2, {["Transparency"] = 0})
					fadeOut:Play()
				end)
			end
		end
	end
end)
2 Likes

That worked! Thank you so much for the help.

1 Like

why can’t you just make it reverse in the tween info?

1 Like
if v:IsA("BasePart") or v:IsA("Decal") then
	local fadeTween = TweenService:Create(v, TweenInfo.new(time, easingStyle, easingDirection, 0, true), {["Transparency"] = 1})
	fadeTween:Play()
end

As suggested here you should be utilising TweenService:Create's ‘reverses’ parameter.

2 Likes

I already tried reversing the tween but I needed an effect where the player would disappear instantly then reappear overtime. I needed two differing time variables for the effect to work best visually.

1 Like