Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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

1 comment

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?

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

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

--cframe stuff

end
end))

This discussion is preserved as it was. New answers, comments, and votes are closed.