How does coroutine.yield work?

  1. What do you want to achieve?
    I am using coroutines to stop and start enemy attacks in my game. I am trying to use coroutine.yield to stop enemy attacks, but I can’t figure out how that works. The documentation for coroutine (coroutine, yield is at the bottom) is terrible and barely explains it.
  2. What is the issue?

    This is confusing to me. I don’t know what a “calling coroutine” is, and I don’t know what the Tuple is that I have to pass in is either.

This is what I have been doing:

-- Round end: remove enemies
gameEvents.RoundEnd.Event:Connect(function()
    print("Round end: ".. #enemies)

    for i, v in pairs(enemies) do
        coroutine.yield(v.AttackCoroutine)  -- v.AttackCoroutine is a coroutine
    end

    enemies = {}
end)

The result of this is that only the first enemy gets stopped. I think coroutine.yield has been yielding the entire function, but I’m not sure.

Well, coroutine.yield waits for the yielded coroutine to be called again before continuing.

Screenshot from Beginners Guide to Coroutines

1 Like