|
| 07 Mar 2012 08:23 PM |
How would I end a while loop 5 seconds after it plays?
while value == 0 do wait() camera.CoordinateFrame = CFrame.new(target.Position) * CFrame.Angles(0, angle, 0) * CFrame.new(5, 10, -5) angle = angle + math.rad(1) end
wait(5) value = 1
This won't work. |
|
|
| Report Abuse |
|
SDuke524
|
  |
| Joined: 29 Jul 2008 |
| Total Posts: 6267 |
|
| |
|
| 07 Mar 2012 08:32 PM |
The reason that it doesn't work is because of how flow control works in scripts.
By default, scripts have a single thread that they run on. Think of a thread as a bunch of instructions that you do one at a time. You must do the instructions in order, and you can't move on to the next one until you finish the one that you're on.
So here's what happens in your script:
-check if value == 0 -wait for a small amount of time -change the camera's cframe -go back to the top of the loop -check if value == 0
and so on and so forth.
It will never get to the part under the while loop because the while loop will never end. It will keep repeating those instructions.
There are two things that you can do:
1. Make the while loop run on a different thread, so that the rest of the stuff on the current thread can keep running without interruption. You can do this using the Spawn function, the delay function, or the coroutine library.
2. Make the condition of the while loop become false somewhere in the while loop.
I'm going to show an example of number 2, because it's funner that way.
time = 0 while time < 5 do -- time will represent the number of seconds passed time = time + wait() -- wait returns the time it has waited in seconds. this will count the time variable up until we have waited 5 seconds. camera.CoordinateFrame = CFrame.new(target.Position) * CFrame.Angles(0, angle, 0) * CFrame.new(5, 10, -5) angle = angle + math.rad(1) end
So there you go, that should work. It makes a variable named `time`, and every time the while loop runs, time goes up by the amount of time that the while loop waited, so once the while loop has waited for 5 seconds, time will be 5, and the loop will end.
@SDuke: but while loops are fun D: |
|
|
| Report Abuse |
|
miz656
|
  |
| Joined: 19 Jul 2010 |
| Total Posts: 15336 |
|
|
| 07 Mar 2012 09:50 PM |
@SDuke
local five = 0 while five <=4 do wait(1) five = five + 1 if five == 5 then break end end
local five = 0 while wait(1) do five = five + 1 if five == 5 then break end end
But I think you meant for loop. |
|
|
| Report Abuse |
|