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

Why will my script not stop looping?

Asked by 9 years ago

I am basically making an update log that has SurfaceGui's and you'll have to click a button, causing the part to move up from the ground into view. If more information is needed, Comment please.

Regular Script:

OpenedOrClosed = game.Workspace.UpdateLog["Open/Closed"].Value
bool = true
--213, 169.4, 62

--213, 180.4, 62
script.Parent.MouseButton1Click:connect(function()
    if OpenedOrClosed == "Closed" then
        local Updatelog = game.Workspace.UpdateLog
        while bool == true do
            Updatelog.CFrame = Updatelog.CFrame * CFrame.new(0,0.1,0)
            wait(0.01)
            if Updatelog.CFrame == CFrame.new(213, 180.4, 62) then
                bool = false
                OpenedOrClosed = "Opened"
            end
        end
    end
    if OpenedOrClosed == "Opened" then
        local Updatelog = game.Workspace.UpdateLog
        while bool == false do
            Updatelog.CFrame = Updatelog.CFrame * CFrame.new(0,-0.1,0)
            wait(0.01)
            if Updatelog.CFrame == CFrame.new(213, 169.4, 62) then
                bool = true
                OpenedOrClosed = "Closed"
            end
        end
    end
end)
0
When I click this, it does rise up but never stops. alphawolvess 1784 — 9y
0
What I can already tell you is that wait(0.01) is the same as wait(), since 0.03 is the minimum argument for wait(). aquathorn321 858 — 9y

2 answers

Log in to vote
0
Answered by
duckwit 1404 Moderation Voter
9 years ago

Your code indentation is erroneous and hence it is harder to observe that you have mismatched ends.

I don't understand why, on line 12, you return if the condition is true. I don't think you meant to type that, but rather:

 if Updatelog.CFrame == CFrame.new(213, 180.4, 62) then
      bool = false
      OpenedOrClosed = "Opened"
end

It's difficult to understand what a lot of your code is supposed to do. At the moment, it continually loops because (I'm guessing) in the second loop, the condition "bool == false" is never reached.

It may be that you only want to trigger the Opened/Closed behaviour separately, in which case you can use an elseif check (on line 18) rather than plain if; that way, when the user clicks, only the code for 'Opened' or 'Closed' will be triggered, but not both.

Another minor point is that, when dealing with boolean values in conditions, someVariable == true is the same as writing someVariable and `someVariable == false' is the same as writing 'not someVariable'. So, for example, your while loop on line 9 could be written:

while bool do
--code
end

Summary

  • Indent your code properly. This will help you identify mismatched ends and will help others be able to read your code easier.
  • Think about if you need to use if or elseif
  • In future questions, try and explain what is supposed to happen in more detail
0
Sorry, I accidentally copied a mess up I did and fixed in game. I'll remove that return end problem alphawolvess 1784 — 9y
Ad
Log in to vote
-3
Answered by 9 years ago

Just Saying but i think it does not like the line

 while bool == true do

Because it mite think that your saying while true do

while true do loops stuff.

0
Incorrect: The 'while ARG do' loop, for it's Argument, will run while it's Argument stays/is true, but when it's Argument becomes false to it, it will stop the loop. TheeDeathCaster 2368 — 9y

Answer this question