How would i go about cancelling moves?

this is for ability cancelling

how would i go about cancelling a move (while its running) from the outside?
ive used coroutines and task.spawn, but im trying to get a jjs effect where the function yields and THEN the function returns the cooldown

my utility script:

module.RegisterMove = function(info)
    local Player = info.Player
    local Function_ = info.Function

    if not Player or not Function_ then
        return 0
    end

    if Moves[Player] then
        module.CancelMove({Player = Player})
    end

    local PlayerFunc = Function_(info.Sending or {})
    if not PlayerFunc.Move then
        return 0
    end

    local cooldown = PlayerFunc.Move()

    Moves[Player] = {
        Move = info.MoveName,
        Cooldown = cooldown or PlayerFunc.Cooldown or 0
    }

    return Moves[Player].Cooldown
end

an example function:

module["YOU FOOL!"] = function(info)
    local function move()
        info.Player:SetAttribute("CanAttack", false)
        print("you fool!!!")
        wait(5)
        print("done")
        info.Player:SetAttribute("CanAttack", true)
        return 65
    end

    return {Move = move}
end

Here’s an overview of what you could do to implement this utilizing your example function:

module["YOU FOOL!"] = function(info)
    local function move(state)
        info.Player:SetAttribute("CanAttack", false)
        print("you fool!!!")

        -- wait 5 seconds, but allow cancellation
        local elapsed = 0
        local duration = 5

        while elapsed < duration do
            if state.Cancelled then
                print("move cancelled")
                info.Player:SetAttribute("CanAttack", true)
                return 0
            end

            local dt = task.wait()
            elapsed += dt
        end

        print("done")
        info.Player:SetAttribute("CanAttack", true)
        return 65
    end

    return {Move = move}
end

Although the utility script could include:

move.CancelMove = function(info)
    local Player = info.Player
    local state = Moves[Player]
    if not state then return end

    state.Cancelled = true
end

module.RegisterMove = function(info)
    -- rest of the code
    if Moves[Player] then
        module.CancelMove({ Player = Player })
    end

    local state = {
        Player = Player,
        MoveName = info.MoveName,
        Cancelled = false,
    }
    Moves[Player] = state
end

Code I provided is just an example of how to do this, there are multiple ways although this is just simple code to push you along.

  1. ive figured it out, instead of making a system that automatically handles the cooldown, i could just make a function that sends a cooldown when its cancelleld or finished

  2. ^ the function will be fired INSIDE of the function which is wrapped in a task.spawn