It sounds like you're using the same name for the button for all of the NPCs. You didn't paste enough code to be sure, but I'd bet you're doing something like
PHP Code:
function BuyHat_Buy.onAction() {
triggeraction(this.x,this.y,"BuyHat","",this.hat_name,this.hat_price);
}
This function is going to be called on every script in the level. The easiest way to fix it is to specify the NPC when creating the window:
PHP Code:
// in the npc that creates the window
BuyHat_Window.npc = this;
and then in the event:
PHP Code:
function BuyHat_Buy.onAction() {
if (BuyHat_Window.npc == this) {
triggeraction(this.x,this.y,"BuyHat","",this.hat_name,this.hat_price);
}
}