Stopping Boss Attacks When Dead

Alright so, I am making a boss system. There is a loop that goes through three attacks that the boss performs. I want it so that once the boss is dead, the attacks end. The problem is that a while ___ do loop only checks the ___ at the end of each loop. I want it so that even if the boss is half way through an attack, he will stop in his tracks, so that I can do something like a death cinematic.

BASICALLY, I want to stop a loop on the event of the boss dying, and it needs so stop everything it’s doing, and return the boss to an idle.

Here’s what I was trying to do:

local function waitWithCheck(length)
	for i = 0, length/0.1 do
		task.wait(0.1)
		if boss.Health.current.Value == 0 then return true end
	end
	return false
end

-- behaviour loop
local function gameLoop()
	print("loop")
	while true do
		if waitWithCheck(5) then break end
		
		voidRun()
		
		if waitWithCheck(5) then break end
		
		cry()

		if waitWithCheck(5) then break end
		
		throwTrash()
	end
	
	hum.Health = 0
end

-- on event
repeat task.wait() until script.start.Value == true

print("recieved")
gameLoop()

This works a little bit, instead of using task.wait() inbetween attacks, I made a function that continues to check the boss health, and then breaks the function. The problem with this is that if the boss is midway through an attack, it wont be able to break the loop until the next waitWithCheck().

Any help at all, is appreciated!

You can try using the Humanoid.Died function, assuming the boss is a humanoid.

i can do somthing like that but it doesnt stop the attack loop from running

try to print the boss Health Value, as it may not be exactly 0

if you put while health > 0 instead of while true do, it will stop the attacks

Just try breaking the loop after the humanoid dies using break