So I have lots of pet NPCs in the game of different kinds, so I tagged them with CollectionService and have scripts that animate them according to their tag. So all the NPCs tagged “Cat” get one animation script, “Dog” gets another, etc.
But I am having a weird problem. The script seems to try to continue running AFTER the tagged model has been destroyed. After a tagged NPC gets destroyed, the code then keeps returning this error:
ServerScriptService.Pet Scripts.Cat Script:73: attempt to index nil with ‘position’
The line of code simply references the PrimaryPart of the NPC. I don’t think the whole code is relevant so I won’t bother pasting it here. I am applying the code to the NPCs using this function:
Do I need to disconnect this function for an NPC when that NPC is destroyed? I thought destroying a model disabled all of the events connected to it. No?
Things I have ruled out:
The pet is not still in the workspace after destroyed somehow.
The pets of the same kind are not being confused with each other.
I tried if pet = nil, then disconnect function … but that didn’t work either.