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

How do I combine a while loop within a block of running code?

To be more specific, I have this code:

if not db then
	      if human and hat then
		
			db = true
			
			Alarm.Noise:Play()
			while x < 10 do
				Alarm.BrickColor = BrickColor.new("Really red")
				wait(0.5)
				Alarm.BrickColor = BrickColor.new("White")
				x = x+1
			
			
			
	    myVar.Text = ("HOW DARE YOU WEAR THAT WRETCHED HAT!?")
	    wait(3)
		myVar.Text = ("YOU WILL BE SMITED BY MY WRATH!")
		wait(3)
		myVar.Text = ("DIE!!")
	    wait(2)
		human.MaxHealth = 0
		wait(3)
		myVar.Text = ("  ")
		
		game.Workspace.Alarm.Noise:Stop()
		
	     end
		
		db = false

This is part of what I have scripted so far. The problem is, that once the first change of color happens, it stops and continues on with the script. A quick run down: Detects if hat and human are true, changes db to true, then the brick changes red to white, then it stops flashing, and runs all the way through the rest of the code. So, what do I do, to make the changing/flashing color of the brick, work through out the block of code? All help is appreciated!

1 answer

THE PROBLEM

The while loop only runs once because it doesn't end properly, so it won't loop. You have also not added a wait() function to the end of the loop, so it will run the loop faster than you can see. Always remember to add a wait() to the end of your loop if you want to show visible changes.

THE SOLUTION

while x < 10 do
                Alarm.BrickColor = BrickColor.new("Really red")
                wait(0.5)
                Alarm.BrickColor = BrickColor.new("White")
                x = x+1
		wait()
end

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