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.
#How can I make it where you CANNOT spawn multiple of the same custom entity? (resolved)
1 messages · Page 1 of 1 (latest)
This can be done, but how to do it depends on how “rare” the NPC should be. It cannot appear when there is the same loaded one, or when there is the same one but not loaded, but when it dies a new one may appear, or if it appears, then under no circumstances will a new one appear
Yup! The NPC is invulnerable so theoretically it shouldn’t be able to spawn again
It’d just be strange to have multiple of the same character walking around
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)
}
})
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!
Thiiis didn't work for me, is there anything up there besides minecraft:pig that I'm supposed to replace? this is supposed to go in my main.js file right? is there anything I need to import that I'm missing? I've already imported world and system
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
it works now that I've removed the part that sets it to false lol