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

How do you stop a loop from an if statement?

So I want to stop a loop from an if statement (BTW this is a boss battle script) Plus I tried Break but scene it's in a function it didn't work

while heathgood do
		script.Parent.ParentHumanoid.HealthChanged:connect(function()
		if script.Parent.Parent.Humanoid.Health < 50 debounce then 
			heathgood = false
			--Do stuff
		end
	end)

	--Do stuff
end

3 answers

✓ Accepted answer

You shouldn't be :connecting inside the loop. You should :connect before the loop.

In order to stop it, just change healthgood to false, so the while loop will stop.

healthgood = true

script.Parent.ParentHumanoid.HealthChanged:connect(function()
	if script.Parent.Parent.Humanoid.Health < 50 debounce then 
		heathgood = false
		--Do stuff
	end
end)

while heathgood do
	--Do stuff
end

You can't use break, because the function() end could be running before or after the loop stops. (It happens whenever the event happens) -- so it's not clear what break would mean.

2 comments

0

Again I did this the loop stops but the first part just keeps on going

0

Without more information, I can't help you. Show your code.

Try this :

function HealthChanged(Health)
	
	if Health < 50 then
		heathgood = false
		
	elseif not (Health < 50) then
		heathgood = true
		
	end
	
end

script.Parent.Parent.Humanoid.HealthChanged:connect(HealthChanged)

I just connected a function to the event "HealthChanged", assuming you are using a Humanoid.

Use the break statement. It basically allows you to "get out" of a for/while/repeat loop. If you need more info on it, look here.

2 comments

0

I tried that but it just siad no loop to break out of

0

well it's in a function in a with a while loop nearby

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