Centralising NPC animations into 1 simple core script?

Currently I have a dozen or so of these scripts placed inside NPC’s around my map, but this like a waste of scripts (I don’t like duplicate scripts :grimacing:)

local Humanoid = script.Parent.Humanoid

Humanoid:LoadAnimation(script.Wave):Play()

And this is very limited in terms of changing their animations later on. So I’m wondering what’s the most efficient way to control all NPC animations in a single script? and allow individual NPC’s to change their animation when required?

1 Like

Create a folder in workspace with every NPC in it and depending on however the animation is activated or whether it’s automatically activated you can have it connected via click / touch event or run it on a timer. But if it’s just a random wave every x seconds all you have to do is run a loop that gets children of the folder and loads the wave animation every x seconds.

In my opinion a neater solution would be to use CollectionService (surprise).

You can iterate through a Folder containing all of your NPC’s, and use the :AddTag(NPCModel, "NPC") method to tag them. Once they’re tagged simply use GetTagged, iterate through the table it returns and play the animation.

local CS = game:GetService("CollectionService")

CS:AddTag(workspace.NPCModel, "NPC")

for _,NPCCharacter in pairs(CS:GetTagged("NPC")) do
    NPCCharacter.Humanoid:LoadAnimation(script.Wave):Play()
end

2 Likes