generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripting Helpers
Home Search
 

Re: While

Previous Thread :: Next Thread 
jimmybolt77 is not online. jimmybolt77
Joined: 06 Jul 2008
Total Posts: 292
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 is not online. SDuke524
Joined: 29 Jul 2008
Total Posts: 6267
07 Mar 2012 08:31 PM
not use a while loop...
Report Abuse
crazytomato4 is not online. crazytomato4
Joined: 09 Feb 2011
Total Posts: 1368
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 is not online. 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
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripting Helpers
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image