How to stop a function with a connected function

Hello,
I am trying to get a function to stop when the humanoid’s health changes but don’t know how to stop it.

Example

local function loop()
     humanoid:GetPropertyChangedSignal("Health"):Connect(function()
          --Stop the function loop() here
     end)

     local i = 0
     while true do
          i += 1
          print(i)
          wait(0.05)
     end
end

Notes

  • I do not want to do an if statement inside of the while loop, I want the connected function to stop the loop() function itself.
  • I tried a return where it says to stop loop() already but that does not work because it is just returning to the connected function and not the function it is nested within.
local stopLoop = false

local function loop()
	stopLoop = false
	
	humanoid:GetPropertyChangedSignal("Health"):Connect(function()
		stopLoop = true
	end)
	
	if stopLoop then
		return
	end
	
	local i = 0
	while true do
		i += 1
		print(i)
		wait(0.05)
	end
end

this won’t stop loop() because you created a function inside task.spawn()

local function loop()
  local c
  local loop = true

  c = Humanoid:GetPropertyChangedSignal("Health"):Connect(function()
    loop = false
    c:Disconnect()
  end)

  while loop do
    ...
  end
end

I know, hence it was edited before you replied, I was toying around in studio.

image

This would work but the loop is just an example. When I actually use this there will be no loop. I am trying to avoid using if statements every few lines to check if I should stop the function.

Any way of doing this without having to do if statements throughout the code?

Can you provide another code example so we know what you mean? I can probably refactor the code above to be functional driven so you just pass an event and function in and let the code do its thing.

local function ok()
     Humanoid:GetPropertyChangedSignal("Health"):Connect(function()
          --Stop ok() here
     end)

     --Below is just a bunch of gibberish just for an example
     anim:Play()
     print("hi")
     walkspeed = 15
     boolean = false
     string = "hello"
end

ok will always run to completion in this example, the function never yields so you dont need to stop it.

Luau is not a multi-threaded language, its run a block of code (known as a thread) until it yields, then it moves onto the next one

Its just an example though. Basically what I am trying to see is can I stop the function from the connected function.

I wouldn’t say either of us were being toxic, we were just pointing out faults in the other’s actions. Your comment with a strike-through on the other hand would be considered “toxic”.

The thing is, unless your code yields (maybe through wait), there’s no reason to return out of a function early with an event.

The event will never be fired before your function finishes due to how Roblox handles this stuff

This is unrelated but you can use humanoid.HealthChanged instead of GetPropertyChangedSignal(“Health”), it also has the added bonus of the additional argument which is passed to any connected callback.

https://developer.roblox.com/en-us/api-reference/event/Humanoid/HealthChanged

Im going to be using animation marker events so would I have to check every animation event?