Issue with for i,v in pairs

I’m making a tween fade effect for closing and opening a shop menu.

The 2 text buttons inside the purchases frame are supposed to fade their transparency at the same time, turns out the next one fades it a little later. Here’s what I mean: https://gyazo.com/22492d163e39b72b68293a18eb46c0f2

Parents: https://gyazo.com/c5c9a6dd68f5a7b8f041f8b44f82ee3f

I’ve tried removing the wait cooldown between the buttons fading and then the shop gui coming up, that didn’t work either.

Code:

local Screen_UI = script.Parent.Parent
local Client__ = Screen_UI.Client

local Tween_Service__ = game:GetService("TweenService")

local __Buttons_ = Client__:WaitForChild("Buttons")
local Player = game.Players.LocalPlayer

local Remotes__ = game.ReplicatedStorage.UIRemotes

local TopBarMenu_Opened = false
local TopBarWindow = Client__.TopBar.TopBarWindow

local Tween_Info = TweenInfo.new(0.3, Enum.EasingStyle.Linear)

local Purchases = script.Parent.Parent.Client.TopBar.TopBarWindow.Window.Purchases:GetChildren()







function Disable_Buttons()
	for i,v in pairs(__Buttons_:GetChildren()) do
		if v:IsA("TextButton") then
			if v.Window_Button.Value == true then
				Tween_Service__:Create(v, Tween_Info, {BackgroundTransparency = 1}):Play()
				Tween_Service__:Create(v.Icon, Tween_Info, {ImageTransparency = 1}):Play()
				Tween_Service__:Create(v.DropShadow, Tween_Info, {ImageTransparency = 1}):Play()
				Tween_Service__:Create(v.Tip, Tween_Info, {TextTransparency = 1}):Play()
				Tween_Service__:Create(v.Tip, Tween_Info, {BackgroundTransparency = 1}):Play()
				wait(.2)
				v.Hover_Image.ImageTransparency = 1
				v.Visible = false
			end
		end
	end
end

Remotes__.DisableButtons.Event:Connect(Disable_Buttons)

function Enable_Buttons()
	for i,v in pairs(__Buttons_:GetChildren()) do
		if v:IsA("TextButton") then
			if v.Window_Button.Value == true then
				v.Visible = true
				Tween_Service__:Create(v, Tween_Info, {BackgroundTransparency = 0}):Play()
				Tween_Service__:Create(v.Icon, Tween_Info, {ImageTransparency = 0}):Play()
				Tween_Service__:Create(v.DropShadow, Tween_Info, {ImageTransparency = 0.85}):Play()
				Tween_Service__:Create(v.Tip, Tween_Info, {TextTransparency = 0}):Play()
				Tween_Service__:Create(v.Tip, Tween_Info, {BackgroundTransparency = 0}):Play()
			end
		end
	end
end

Remotes__.EnableButtons.Event:Connect(Enable_Buttons)

function Open_TopBarMenu()
	if TopBarMenu_Opened == false then
		TopBarMenu_Opened = true
	for i,v in pairs(Purchases) do
		if v:IsA("TextButton") then
				Remotes__.DisableButtons:Fire()
				wait(0.5)
				TopBarWindow.Visible = true
				Tween_Service__:Create(v, Tween_Info, {BackgroundTransparency = 0}):Play()
				Tween_Service__:Create(TopBarWindow, Tween_Info, {BackgroundTransparency = 0}):Play()
				Tween_Service__:Create(v.text1, Tween_Info, {TextTransparency = 0}):Play()
				Tween_Service__:Create(v.text2, Tween_Info, {TextTransparency = 0}):Play()
				Tween_Service__:Create(v.Icon, Tween_Info, {ImageTransparency = 0.8}):Play()
				Tween_Service__:Create(TopBarWindow.Window.Close, Tween_Info, {TextTransparency = 0}):Play()
				TopBarWindow:TweenPosition(UDim2.new(0.499, 0,3.02, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Quint, 0.4)
			end
		end
	end
	end


__Buttons_.Shop.MouseButton1Down:Connect(Open_TopBarMenu)


function Close_TopBarMenu()
	for i,v in pairs(Purchases) do
		if v:IsA("TextButton") then
			if TopBarMenu_Opened == true then
				TopBarMenu_Opened = false
				Tween_Service__:Create(v, Tween_Info, {BackgroundTransparency = 1}):Play()
				Tween_Service__:Create(TopBarWindow, Tween_Info, {BackgroundTransparency = 1}):Play()
				Tween_Service__:Create(v.text1, Tween_Info, {TextTransparency = 1}):Play()
				Tween_Service__:Create(v.text2, Tween_Info, {TextTransparency = 1}):Play()
				Tween_Service__:Create(v.Icon, Tween_Info, {ImageTransparency = 1}):Play()
				Tween_Service__:Create(TopBarWindow.Window.Close, Tween_Info, {TextTransparency = 1}):Play()
				TopBarWindow:TweenPosition(UDim2.new(0.919, 0,3.02, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Quint, 0.4)
				wait(0.4)
				Remotes__.EnableButtons:Fire()
				TopBarWindow.Visible = false
				TopBarWindow.Position = UDim2.new(0.119, 0,3.02, 0)
			end
			end
		end
end

TopBarWindow.Window.Close.MouseButton1Down:Connect(Close_TopBarMenu)


Really would appreciate some help asap.

the code is very hard to read and understand, BUT

it seems as if you were firing that in the for loop which would make the background fade before the ‘second’ tween is played.
solution: put this after the loop :smiley:

Thanks for the tip! Am I supposed to put if TopBarMenu_Opened == false/true
after the loop?

well, obviously yesh x3 (chars)

The problem is that its tweening the first frame, then waiting for it to be finished, then tweening the second frame. An easy solution to fix this is to use coroutine which creates a new thread so you don’t have to wait for the first frame to be finished tweening.

function Open_TopBarMenu()
	if TopBarMenu_Opened == false then
		TopBarMenu_Opened = true
	for i,v in pairs(Purchases) do
		if v:IsA("TextButton") then
          coroutine.wrap(function()
				Remotes__.DisableButtons:Fire()
				wait(0.5)
				TopBarWindow.Visible = true
				Tween_Service__:Create(v, Tween_Info, {BackgroundTransparency = 0}):Play()
				Tween_Service__:Create(TopBarWindow, Tween_Info, {BackgroundTransparency = 0}):Play()
				Tween_Service__:Create(v.text1, Tween_Info, {TextTransparency = 0}):Play()
				Tween_Service__:Create(v.text2, Tween_Info, {TextTransparency = 0}):Play()
				Tween_Service__:Create(v.Icon, Tween_Info, {ImageTransparency = 0.8}):Play()
				Tween_Service__:Create(TopBarWindow.Window.Close, Tween_Info, {TextTransparency = 0}):Play()
				TopBarWindow:TweenPosition(UDim2.new(0.499, 0,3.02, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Quint, 0.4)
			end)()
          end
		end
	end
end
1 Like

Okay. Will try that. I’ll let you know if it fixes the issue.

Oh that’s neat. That worked for me. Thanks!

1 Like

Hey there, Where did you learn about co-routines? Did you find someone on youtube/other that made it easy to follow?

I learned about them a long time ago so I don’t remember. You can probably find some tutorials on the devforum or youtube that will help you.

Cool. I’ll check around. They are next on my list of stuff to learn and I thought I’d ask.

Thanks for the response!

2 Likes