I've made simple userscript for the game https://zty.pe/, The bot work on my modified version of the game, I'm trying to make it whenever I click "F" it starts running , but stops when I click " C ", the loop works normally after clicking C and it doesnt stop.
document.addEventListener('keydown', (e) => {
key = e.keyCode
if(key == 70){
var zTypeBotAlphaChars = "abcdefghijklmnopqrstuvwxyz";
var zTypeBotCharToType = 0;
ztypebot = setInterval(function() {
ig.game.shoot(
zTypeBotAlphaChars.charAt(zTypeBotCharToType));
}
, 10);
}
})
document.addEventListener('keydown', (e) => {
if(e.keyCode == 67){
clearInterval(ztypebot, 10);
}
})
Try adding window. before ztypebot interval to make it a global function:
document.addEventListener('keydown', (e) => {
key = e.keyCode
if(key == 70){
var zTypeBotAlphaChars = "abcdefghijklmnopqrstuvwxyz";
var zTypeBotCharToType = 0;
window.ztypebot = setInterval(function() {
ig.game.shoot(
zTypeBotAlphaChars.charAt(zTypeBotCharToType));
}
, 10);
}
})
document.addEventListener('keydown', (e) => {
if(e.keyCode == 67){
clearInterval(window.ztypebot, 10);
}
})
Your ztypebot variable is local in each function, you need to define it outside like so:
let ztypebot;
document.addEventListener('keydown', (e) => {
...
ztypebot = setInterval(function() {
...
}
})
document.addEventListener('keydown', (e) => {
if(e.keyCode == 67){
clearInterval(ztypebot, 10);
}
})