# Whats wrong with this function?

**URL:** <https://forum.kirupa.com/t/whats-wrong-with-this-function/268721>\
**Category:** Uncategorized\
**Created:** [August 18, 2008, 3:07am UTC](https://forum.kirupa.com/t/whats-wrong-with-this-function/268721 "2008-08-18T03:07:01Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![DesignerMichael](https://avatars.discourse-cdn.com/v4/letter/d/898d66/32.png) [@DesignerMichael](https://forum.kirupa.com/u/DesignerMichael)\
**Post date:** [August 18, 2008, 3:07am UTC](https://forum.kirupa.com/t/whats-wrong-with-this-function/268721/1 "2008-08-18T03:07:01Z")

</div>

This is a serverside AS1 function keeps messing up and looping past the number of players after the last player is dead… Any ideas how to best do this?

```auto
var playerTurn=0;
var stopLoop=0;
function nextTurn() {
    turnTick=0;
    stopLoopAt = room.getMaxUsers();
    while (Players[playerTurn].Alive == false && stopLoop < stopLoopAt) {
        ++playerTurn;
        ++stopLoop;
    }
    takeTurn(Players[playerTurn].sfsId);
    currentTurn=playerTurn;
    ++playerTurn;
    if (playerTurn == stopLoopAt){
        playerTurn = 0;
    }
}

```
