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

How to make Lua wait for a for loop to finish?

I have a Gui menu. I want it so that when the player press 'Start', the menu buttons will fly away as the screen turns to black and while the screen is black, the camera will switch from the menu to the player.

-- 'frame' is the Gui that turns the screen black
if menu then
		transition = true
		for i=1,0,0.01 do -- make the frame go from transparent to opaque
			wait()
			frame.BackgroundTransparency = i
		end
		for _,gui in pairs(menu:GetChildren()) do -- make the menu guis fly out
			gui:TweenPosition(UDim2.new(-1, 0, 0, 0), "Out", "Quint", 5)
		end

		wait(3) -- to make the screen stay dark for a while
		
		cam.CameraSubject = player.Character.Humanoid -- give the camera back to the player
		cam.CameraType = "Custom"

		for i=0,1,0.01 do -- turn the gui from opaque to transparent
			wait()
			frame.BackgroundTransparency = i
		end
		transition = false
	end

This plays out in my head nicely but, when I press 'start' in the game everything fires off in totally the wrong order in such a messy way that makes my eyes sad. First of all, the Gui flies away before the screen is opaque (creating an awkward pause) and then the camera seems to switch before the Gui stops being opaque. It's like everything I'm doing is ignoring the for loop at the top of the code and going whenever they feel like it. Here is a gif to make things a bit clearer: http://imgur.com/4BdOias

1 answer

✓ Accepted answer

I'm not sure how that happens since the loop for the opaque is before the tween

Maybe try putting a minus infront of the 0.01

for i=1,0,-0.01 do
            wait()
            frame.BackgroundTransparency = i
        end

2 comments

0

That worked perfectly. Thank you. What exactly did I do wrong there?

0

You forgot the Minus, since its going down (1-0) it needs to minus -0.01.

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