luarepeatrobloxtween

How do I prevent my tween from playing double?


I have a simple project where I'm making a card battling game. The battle sequence looks like this:

     repeat

        BattleClient.PlayerAttackTween()
        task.wait(battleSpeed)
        BattleClient.EnemyAttackTween()
        task.wait(battleSpeed)
        battleRound += 1

    until isInBattle.Value == false

The issue I'm having is that if the player starts another battle right after finishing one, this loop is still in the middle of a task.wait() and has not yet gotten to the until check to break the loop, so the tweens play double during that second fight.

How do I prevent this from happening?

Edit: Video showcasing issue


Solution

  • IDK the whole function/loop but try this algorithm :

    local isBattleActive = false -- new binary flag
    
    function StartBattle()
        if isBattleActive then return end -- battle active :do not start
        isBattleActive = true
    
        repeat
            BattleClient.PlayerAttackTween()
            task.wait(battleSpeed)
            BattleClient.EnemyAttackTween()
            task.wait(battleSpeed)
            battleRound += 1
        until isInBattle.Value == false
        
        isBattleActive = false -- battle over, reset flag
    end