Jump to content

async hook at json inside of setInterval


erwinweber

Recommended Posts

Hello guys,

I am trying to make a function that returns if a player is located around a marker/position at a certain radius. The first solution in my mind was to set an interval in which it checks every 5 sec or so if the player is inside the radius of a position. But if I put the json/object inside the setInterval function, the server crashes when this function is called. I know for sure the json is the problem, beacuse if i just put a console.log inside the setInterval, it works fine.

Error: async hook stack has become corrupted (actual: 10, expected: 11)

There's other code going on than just this piece, I thought this might be enough to see what's going. Many thanks in advance for your help. Have a great day! Erwin

let positions = require('./positions.json');
 
function startCheckingCaps(player){
    setInterval(function(){
        positions.cap.forEach(function(v,i){
            if (inRadius(v)){ //inRadius checks if player.position is inside the radius of the current position 'v'
                player.notify("capturing " + v.name);
            }
            else {
                player.notify("not capturing");
            }
        });
       //console.log("if its just this in setInterval, it works just fine.");
    }, 5000); 
}
 
mp.events.add('playerJoin', (player) => {
   startCheckingCaps(player); //<------------------------- this is the one from above
});
Link to comment
Share on other sites

Fixed making these modifications. Thanks to kemperrr and Splak from Discord!

function startCheckingCaps(player){
    player.capTimer = setInterval(() => {
        try {
            if (!mp.players.exists(player) && PlayerLocal.team == null) {
                clearInterval(player.capTimer);
                return false;
            }
 
            positions.cap.forEach((v, i) => {
                if (v.name != undefined) {
                    if (inRadius(v, player)){
                        player.notify("capturing " + v.name);
                    }
                    else {
                        player.notify("not capturing");
                    }
                }
            });
        } catch(e) {
            console.log(e);
        };
    }, 5000);
}
 
 
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...