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
local function loop()
local c
local loop = true
c = Humanoid:GetPropertyChangedSignal("Health"):Connect(function()
loop = false
c:Disconnect()
end)
while loop do
...
end
end
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.
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
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”.
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.