#How can I make it where you CANNOT spawn multiple of the same custom entity? (resolved)

1 messages · Page 1 of 1 (latest)

dense ruin
#

I would like my custom NPC to not be able to be spawned again after there’s already one existing in the world. I’m planning on giving it a ticking area, so it’ll always be loaded if that’s helpful. I would like the game to refuse to summon another one when one is already existing in the world. I’ve seen something similar in another addon, so I know it’s possible and more likely than not is scripting related, but I’m pretty new to and and I’m unsure of how to pull it off.

unique pike
dense ruin
#

It’d just be strange to have multiple of the same character walking around

unique pike
#
const NPCID = 'minecraft:pig'
world.afterEvents.entitySpawn.subscribe(data=> {
  let entity = data.entity
  if (data.cause == 'Spawned' && entity.typeId == NPCID) {
    if (world.getDynamicProperty('npcExists')) {
      entity.remove()
    } else {
      world.setDynamicProperty('npcExists', true)
    }
  }
})

world.afterEvents.entityRemove.subscribe(data => {
  if (data.typeId == NPCID) {
    world.setDynamicProperty('npcExists', false)
  }
})
dense ruin
#

And I should just put my npc’s tag where the pig one is?

#

Awesome! I’ll have to try this when I get to my computer 🙂 thank you!

dense ruin
#

well, I think it works sometimes, like I'll see a copy of the npc flash on screen for a moment before disappearing, but other times it just doesn't work

dense ruin
#

it works now that I've removed the part that sets it to false lol