How to cancel a function when health lowers?

Im trying to make a combat system where you cannot do anything while being stunned, by having strings on the character and the skills check if your string is “Stunned” in order to cast them. However, when trying to make your stun go away after a second if your health hasnt changed, the part that makes it cancel the function when health is changed doesnt work. I also dont know how to make it detect only health decreasing, instead of just any change in health.

remote.OnClientEvent:Connect(function(what)
	if what == "Stunned" then
		blockstate.Value = "Stunned"
		Character.Humanoid.WalkSpeed = 3
		spawn(function(reset) -- as soon as you're stunned starts the reset function
			
			Character.Humanoid.HealthChanged:Connect(function(changed)  --if health changes then cancel the rest
				print("health changed")
				if changed then return end
			end)
			
			wait(1) --if not hit after 1 second continues to reset the block
			print("Normal state")
			blockstate.Value = "Normal"
			Character.Humanoid.WalkSpeed = 16
				
		end)
	end
end)

You should have it as a variable and then do variable:Disconnect()

Have what as a variable? I never used disconnect() and my attempts to use it result in “Attempted to index nil with Disconnect()”

local connection 

remote.OnClientEvent:Connect(function(what)
	if what == "Stunned" then
		blockstate.Value = "Stunned"
		Character.Humanoid.WalkSpeed = 3
		function reset() -- as soon as you're stunned starts the reset function
			
			Character.Humanoid.HealthChanged:Connect(function(changed)  --if health changes then cancel the rest
				print("health changed")
				if changed then 
					connection:Disconnect()
				end
			end)
			
			wait(1) --if not hit after 1 second continues to reset the block
			print("Normal state")
			blockstate.Value = "Normal"
			Character.Humanoid.WalkSpeed = 16
				
		end
		reset()
		connection = reset()
	end
end)

I don’t see the line where you disconnected the function…?

Character.Humanoid.HealthChanged:Connect(function(changed)

Bare in mind that you’re creating this connection each time the ‘RemoteEvent’ object is fired (event connections stack not override), you need to ensure that the ‘HealthChanged’ event/signal only has a single connection to it at any given time.

local connection 

connection = remote.OnClientEvent:Connect(function(what)
	if what == "Stunned" then
		blockstate.Value = "Stunned"
		Character.Humanoid.WalkSpeed = 3
		function reset() -- as soon as you're stunned starts the reset function
			
			Character.Humanoid.HealthChanged:Connect(function(changed)  --if health changes then cancel the rest
				print("health changed")
				if changed then 
					connection:Disconnect()
				end
			end)
			
			wait(1) --if not hit after 1 second continues to reset the block
			print("Normal state")
			blockstate.Value = "Normal"
			Character.Humanoid.WalkSpeed = 16
				
		end
	end
end)