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

How do I break an if then statement loop early?

Asked by 4 years ago

I posted a question earlier. I figured out why the script stopped working but now there is a new problem. Thankfully, with the previous one (the link below), the only problem was apparently me forgetting a few ends, and the example will not need a link to it.

https://scriptinghelpers.org/questions/95225/how-do-i-fix-my-npcs-schedule-script

I am trying to make the script like a schedule. Throughout the schedule, I realized a single problem... The code below is a small chunk however what happens is obvious...

When the code runs, it loops the if statement below, and once that loop is over, it will check the time to do the next thing. I am trying to break that loop since it falls on the waits. I want to break the loop out of the wait when the next time frame/activity comes around, instead of the NPC going to the task it was supposed to have done or even skipping tasks based on the time.

I tried using elseif instead of if to see if that would fix the problem, but it does not seem to have worked.

if game.Lighting.ClockTime > 19 or game.Lighting.ClockTime < 5 then
    NPC.Humanoid.Sit = false
    wait(.1)
    NPC:MoveTo(script.Parent.Spawn.Position)
    NPC.Humanoid:MoveTo(script.Parent.Bed.Position)
    NPC.Humanoid.MoveToFinished:wait()
    wait(100)
    elseif game.Lighting.ClockTime > 5 and game.Lighting.ClockTime < 6 then
    --Beginning of day (5:00)
    NPC.Humanoid.Sit = false
    NPC.Humanoid:MoveTo(script.Parent.OutsideofRoom.Position)
    NPC.Humanoid.MoveToFinished:wait()
    wait(0)
    NPC.Humanoid:MoveTo(script.Parent.TopofStairs.Position)
    NPC.Humanoid.MoveToFinished:wait()
    wait(0)
    NPC.Humanoid:MoveTo(script.Parent.BottomofStairs.Position)
    NPC.Humanoid.MoveToFinished:wait()
    wait(0)
    NPC.Humanoid:MoveTo(script.Parent.OutsideReceptionRoom.Position)
    NPC.Humanoid.MoveToFinished:wait()
    wait(0)
    NPC.Humanoid:MoveTo(script.Parent.OutsideBathroom.Position)
    NPC.Humanoid.MoveToFinished:wait()
    wait(0)
    NPC.Humanoid:MoveTo(script.Parent.InsideBathroom.Position)
    NPC.Humanoid.MoveToFinished:wait()
0
I don't quite understand your problem. royaltoe 5144 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

How To Break Out Of An If-Then Statement

Did some research and you cannot use Break to break If-Then Statements, only Loops. To break an If-Then Statement, you would use Return as this will end the function because it will halt the program and stop it from running.

Return Wiki

Quote:

"There is a "break" statement used to terminate loops early and are typically inside of "if" statements, but you can't break out of an if, it only terminates loops like for, while and repeat.

The return statement can be used to terminate a function early. To get out of the middle of an if, you just have to structure the if correctly."

Hope this cleared it up for you!

Ad

Answer this question