How to make animation play for every npc but don't put script on every model?

Hi,
I want to create a script that will play animation for every npc, but i don’t want to put it into every npc model. I want only 1 script that will do everything. Is it possible to make this kind of script?

Iterate over all the NPCs and play the animation for each one.

Some example code:

local npcs = someContainerOnlyHoldingMyNPCS

for i, npc in pairs(npcs) do
    npc.Humanoid:LoadAnimation(animation):Play()
end
1 Like

Even easier would be CollectionService. With this you can still have all NPCs play the animaiton, but if an event happens in one NPC, that one NPC can change the animation and still only use the one collectionService script.

someContainerOnlyHoldingMyNPCS was meant to represent any array derived from somewhere that holds NPCs.

Yeah, you can still use the array, but you could get each object within the array, and tag the with a collectionService tag like “NPC” or something. Combining your array and collectionService could also lead the way for new NPCs added within the game to then follow the animation if they wish, without having to explicitly wait for an NPC to spawn.

wait can i play an animation if its in local script?