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

While loops in while loops don't work. Help?

Asked by 9 years ago

My attempt at making a platform that goes back and forth is considered to be a failure. Is it okay to put a while loop in as while loop? This is my code:

while true do
    while true do
        script.Parent.CFrame=script.Parent.CFrame+Vector3.new(0,0,.5)
        wait()
    end
    script.Parent.BrickColor=BrickColor.new("Bright blue")
    wait(5)
    while true do
        script.Parent.CFrame=script.Parent.CFrame-Vector3.new(0,0,.5)
        wait()
    end
    script.Parent.BrickColor=BrickColor.new("Bright orange")
    wait(5)
end

2 answers

Log in to vote
1
Answered by 9 years ago

A script doesn't move on to the next section until the previous section has yielded. Your script is stuck on the first (nested) while loop. You can however, use coroutines to prevent this. Coroutines

0
So, I can create a coroutine, put my code in there, yield it, and let a section of the code run when I call it? GamermanNotHacked 7 — 9y
Ad
Log in to vote
0
Answered by
funyun 958 Moderation Voter
9 years ago

Put these inside the main loop to replace the other "while " loops

coroutine.resume(coroutine.create(function()
while wait() do

--cframe stuff

end
end))

Answer this question