Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by
alibix123 175
8 years ago

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.

01-- 'frame' is the Gui that turns the screen black
02if menu then
03        transition = true
04        for i=1,0,0.01 do -- make the frame go from transparent to opaque
05            wait()
06            frame.BackgroundTransparency = i
07        end
08        for _,gui in pairs(menu:GetChildren()) do -- make the menu guis fly out
09            gui:TweenPosition(UDim2.new(-1, 0, 0, 0), "Out", "Quint", 5)
10        end
11 
12        wait(3) -- to make the screen stay dark for a while
13 
14        cam.CameraSubject = player.Character.Humanoid -- give the camera back to the player
15        cam.CameraType = "Custom"
View all 22 lines...

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

Log in to vote
1
Answered by 8 years ago

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

1for i=1,0,-0.01 do
2            wait()
3            frame.BackgroundTransparency = i
4        end
0
That worked perfectly. Thank you. What exactly did I do wrong there? alibix123 175 — 8y
0
You forgot the Minus, since its going down (1-0) it needs to minus -0.01. LittleBigDeveloper 245 — 8y
Ad

Answer this question