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 » Scripters
Home Search
 

Re: Resetting Loops

Previous Thread :: Next Thread 
blockoo is not online. blockoo
Joined: 08 Nov 2007
Total Posts: 17202
08 Jul 2014 03:44 PM
I know I shouldn't be posting help topics here, but I am stumped and SH is as well. Anyway, how can I restart a loop when it's in the middle of a cycle? For example, if I have this script:

while true do
wait()
if (x == true) then
--Operation A
wait(2)
if (x == false) then
--return to beginning of loop
else
--Operation B
end
end
end

I marked where I'd like it to return to the beginning of the loop, so that Operation B does not occur if x changes from true to false after the first "if" statement has been passed. Any ideas?
Report Abuse
EchoReaper is not online. EchoReaper
Joined: 14 Oct 2008
Total Posts: 4323
08 Jul 2014 03:56 PM
Recursion is your friend. i.e.

function looper()
          while wait() do
                    if (x == true) then
                              --Operation A
                              wait(2)
                              if (x == false) then
                                       looper()
                                        break
                              else
                                        --Operation B
                              end
                    end
          end
end
Report Abuse
blockoo is not online. blockoo
Joined: 08 Nov 2007
Total Posts: 17202
08 Jul 2014 04:00 PM
Wow, I completely forgot about that. I've been gone too long ;_;
Anyway, thanks
Report Abuse
StealthKing95 is online. StealthKing95
Joined: 13 Dec 2008
Total Posts: 4263
08 Jul 2014 04:00 PM
Theres not even any need for recursion


function loop()
while true do
wait()
if (x == true) then
--Operation A
wait(2)
if (x == false) then
break
end
else
--Operation B
end
end
return true
end

while true do
local success = loop()
end
Report Abuse
Dr01d3k4 is not online. Dr01d3k4
Joined: 11 Oct 2007
Total Posts: 17916
09 Jul 2014 05:32 AM
What you need is a continue statement, however Lua doesn't have that. A workaround I've seen is to use repeat until true.

while (true) do
repeat
...
if (shouldContinue) then
break;
end
...
until (true);
end
Report Abuse
OneTrueGodTheHolyCow is not online. OneTrueGodTheHolyCow
Joined: 24 Oct 2013
Total Posts: 9350
09 Jul 2014 06:09 AM
ugh, Lua sucks, why don't they have some of these things like ++/+=/*=//= and why don't they have continue and I'm probably missing a lot

Report Abuse
Notunknown99 is not online. Notunknown99
Joined: 05 Sep 2008
Total Posts: 25360
09 Jul 2014 06:12 AM
"++/+=/*=//="

I heard that they are "too complex" and "ruin the simplicity"...
Report Abuse
Notunknown99 is not online. Notunknown99
Joined: 05 Sep 2008
Total Posts: 25360
09 Jul 2014 06:14 AM
Looking into it, metatables ruin everything.
Report Abuse
OneTrueGodTheHolyCow is not online. OneTrueGodTheHolyCow
Joined: 24 Oct 2013
Total Posts: 9350
09 Jul 2014 06:23 AM
Whatever, it's super annoying and even C has them
Report Abuse
Notunknown99 is not online. Notunknown99
Joined: 05 Sep 2008
Total Posts: 25360
09 Jul 2014 06:25 AM
It is annoying, and the reasoning behind it implies "I was too lazy to do it"...
Report Abuse
StealthKing95 is online. StealthKing95
Joined: 13 Dec 2008
Total Posts: 4263
09 Jul 2014 06:43 AM
From stackoverflow

The way that the language manages lexical scope creates issues with including both goto and continue. For example,

local a=0
repeat
if f() then
a=1 --change outer a
end
local a=f() -- inner a
until a==0 -- test inner a

The declaration of local a inside the loop body masks the outer variable named a, and the scope of that local extends across the condition of the until statement so the condition is testing the innermost a.

If continue existed, it would have to be restricted semantically to be only valid after all of the variables used in the condition have come into scope. This is a difficult condition to document to the user and enforce in the compiler. Various proposals around this issue have been discussed, including the simple answer of disallowing continue with the repeat ... until style of loop. So far, none have had a sufficiently compelling use case to get them included in the language.
Report Abuse
EchoReaper is not online. EchoReaper
Joined: 14 Oct 2008
Total Posts: 4323
09 Jul 2014 10:08 AM
""++/+=/*=//="

I heard that they are "too complex" and "ruin the simplicity"...

It is annoying, and the reasoning behind it implies "I was too lazy to do it"..."

If there was a like button, I would hit the fk out of it.
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • 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