Stopping a function when attacked

Hello DevForums!
I’m currently making a battlegrounds game that uses tools to represent the attacks a player can do. If I have a tool script like the following, how might I be able to interrupt it when the player is hurt?

local tool = script.Parent

local ReplicatedStorage = game.ReplicatedStorage
local ServerScriptService = game.ServerScriptService
local SpecialAnimations = tool
local AttackHitboxes = require(ServerScriptService.Server.AttackHitboxes)
local SoundEngine = require(ServerScriptService.Server.SoundEngine)
local Effects = game.ServerStorage.Effects
local TweenService = game:GetService("TweenService")


function Attack(Player,Character,Humanoid : Humanoid)
	local Animator = Humanoid.Animator
	local PunchAnim = Animator:LoadAnimation(SpecialAnimations.CPunch)
	--Humanoid.AutoRotate = false

	PunchAnim:Play()
	PunchAnim:GetMarkerReachedSignal("Punch"):Connect(function()
		print("HAYEAHHHH")
		SoundEngine:PlayCharacterSound(Character,"rbxassetid://8595984380",1,false)
		local fist = Effects.Kiren.KirenFist:Clone()
		fist.CFrame = Character.HumanoidRootPart.CFrame * CFrame.new(0,2,-11.5)
		fist.Parent = workspace

		local DamageParameters = {
			Ragdoll = true,
			DamageVelocity = Character.HumanoidRootPart.CFrame.LookVector * 100 + Vector3.new(0,50,0)
		}
		AttackHitboxes:CreatePositionedHitbox(Character,fist.Size,CFrame.new(0,2,-11.5),20,true,DamageParameters)

		local fistTween = TweenService:Create(fist,TweenInfo.new(0.5),{Transparency = 1})
		fistTween:Play()
		fistTween.Completed:Wait()
		fist:Destroy()
	end)

	PunchAnim.Ended:Wait()
	tool:Deactivate()
	--Humanoid.AutoRotate = true
end

tool.Activated:Connect(function()
	local character = tool.Parent
	local humanoid = character:WaitForChild("Humanoid")
	Attack(game.Players:GetPlayerFromCharacter(character),character,humanoid)
end)

As a note, the tool deactivates at the end to tell the UI script the tool is finished attacking.
Thanks!

dont use keyframes, use task.delay etc which does not require an animation, some servers may fail to load it.

you can disconnect the connection / stop the task
ex.; at attribute change, if you switch to delay it can be done if coroutine.status returns suspended, just use task.cancel after