Fixed. Advanced version that doesn't remove player tokens (be sure to set your bar # for HP on line 3): const DropAtZeroHealth = (()=>{
const threshold = 0;
const bar = 1;
const isPlayerToken = (obj) => {
let players = obj.get('controlledby')
.split(/,/)
.filter(s=>s.length);
if( players.includes('all') || players.filter((p)=>!playerIsGM(p)).length ) {
return true;
}
if('' !== obj.get('represents') ) {
players = (getObj('character',obj.get('represents')) || {get: function(){return '';} } )
.get('controlledby').split(/,/)
.filter(s=>s.length);
return players.includes('all') || players.filter((p)=>!playerIsGM(p)).length ;
}
return false;
};
const onChangeGraphic = (obj,prev) => {
const healthBar = `bar${bar}_value`;
const val = parseInt(obj.get(healthBar));
if(val !== parseInt(prev[healthBar]) && val <= threshold ){
if(!isPlayerToken(obj)){
let turnorder = Campaign().get('turnorder');
turnorder = ('' === turnorder) ? [] : JSON.parse(turnorder);
turnorder = turnorder.filter((i)=>i.id !== obj.id);
Campaign().set('turnorder',JSON.stringify(turnorder));
}
obj.set('statusmarkers','dead');
}
};
const registerEvents = () => {
on('change:graphic', onChangeGraphic);
};
return {
RegisterEvents: registerEvents,
OnChangeGraphic: onChangeGraphic
};
})();
on('ready',() => DropAtZeroHealth.RegisterEvents() );