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 make a while loop run through another piece of code?

To be specific, I've got this code I've been working on for a few days, and I'm adding some finishing touches. Here it is:

local part = script.Parent
local db = false
local myVar = game.Workspace.Part.SurfaceGui.Test
local Alarm = game.Workspace.Alarm
local x = 0

	


function FindHat(hit)
	local hat = hit.Parent:FindFirstChild("SnipersVisor")
	local human = hit.Parent:FindFirstChild("Humanoid")
	    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
		
	
		
		else
			myVar.Text = ("You're good to go!")
			wait(2)
			myVar.Text = ("    ")
			
			
		
		
		
		end
	end	
end


part.Touched:connect(FindHat)

Everything works, except the "while" part. I want the brick to change colors, while the text changes on the GUI. On top of it, it loops alot of times. And I know there's an "end" missing to finish the loop, but when I add it, nothing works. Any help, please?

1 comment

0

You choose a terrible answer in your last question. This is your fault.

2 answers

You can change the loop to a for loop I also removed many empty lines, as they are not needed.

local part = script.Parent
local Workspace = game:GetService("Workspace")
local db = false
local myVar = Workspace.Part.SurfaceGui.Test
local Alarm = Workspace.Alarm
--Don't need X value

function FindHat(hit)
	local hat = hit.Parent:FindFirstChild("SnipersVisor")
	local human = hit.Parent:FindFirstChild("Humanoid")
	if not db then
		if human and hat then
			db = true
			Alarm.Noise:Play()
			for i = 1,10 do --Will still run 10 times
				Alarm.BrickColor = BrickColor.new("Really red")
				wait(0.5)
				Alarm.BrickColor = BrickColor.new("White")
			end
			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 = ("")
			Workspace.Alarm.Noise:Stop()
	        db = false
        else
            myVar.Text = ("You're good to go!")
            wait(2)
            myVar.Text = ("")
        end
    end 
end

part.Touched:connect(FindHat)

Not sure as I am new to scripting, but use a for loop for that, as you are counting up

2 comments

0

You can't use a for loop though, since a for loop requires inserting a value (i) into a certain place. And I can't really think of where to put that value. Thanks for trying though.

0

You didn't need to use i, thats just for counting up for instance. Look at the answer above :)

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