How can I set my NPC active with its animation?

HI All,
I am a newbie but I really love developing game in roblox.
I have some NPC models in my workspace .How can I let them active with its animations?
I think maybe I need a server-side script which can instance those NPCs and call the corresponding animations like this (pseudocode):


local gs_workspace = game.getservice(workspace)
local myNPCs = gs_workspace.findEveryNPCsHumanoid()   -- how to ?

for k,v in pairs(myNPCs) then
    local npcAnimation = Instance.new("Animation")
	npcAnimation .AnimationId = "rbxassetid://0000000"    -- the animationId for this v
	 
      local thisNPCInstance = WhoCreateThis(v) --who create those instances by the NPCs humanoid?

	local npcAnimationTrack = v:LoadAnimation(kickAnimation)
	 
	npcAnimationTrack:Play()
end

Need your help and thank you so much!

Ps:
my NPCs will inact with players .So should this script in ReplicatedStorage and then communicate with the players’ localscript ?

2 Likes

Hey! as you said in your help I see that you said that it may need to be a Server Script, this is correct so you should put the code in a server script.

Step 1
Place all the NPC’s into a folder. In Workspace.
image

Step 2
Create a Server Script in ServerScriptService

local Workspace				= game:GetService("Workspace") -- Just gets Workspace
local NPCs					= Workspace:WaitForChild("NPC's"):GetChildren() -- :GetChildren() will get 
all the NPC from the folder

for _,NPC in pairs(NPCs) do -- Loops through the folder from the variable above. _ is the index and 
NPC is the NPC 
    local npcAnimation = Instance.new("Animation") -- Creates the Animation Instance
    npcAnimation .AnimationId = "rbxassetid://507770239" -- Put the ID of the animation in 0000000
npcAnimation.Parent = NPC -- Places npcAnimation inside the NPC
     local npcAnimationTrack = NPC:WaitForChild("Humanoid"):LoadAnimation(npcAnimation) -- Loads 
    the animation
    npcAnimationTrack:Play() -- Plays the animation
end
6 Likes

Thank you so much W0Qk!
I will try it now

Parenting every NPC to a single folder might cause problems for OP (for example, they may appear inside a map), and won’t allow new NPCs to receive the animation. They could, instead, use CollectionService with the Tag Editor plugin to ease development:

-- get collection service
local collectionService = game:GetService("CollectionService")

-- create animation
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://507770239"

-- re-usable function
local function handleNpc(npc)
    local animationTrack = npc.Humanoid:LoadAnimation(animation):Play()
end

-- add to already existing NPCs
for _, npc in ipairs(collectionService:GetTagged("Npc")) do
    handleNpc(npc)
end

-- handle new NPCs being added
collectionService:GetInstanceAddedSignal("Npc"):Connect(handleNpc)

There’s also no need to do this in a server-side script. It would probably be a better idea to handle this completely on the client to reduce the server’s load and create a better overall user experience (with regards to latency).

3 Likes

Good advice

a little question:how can I get the assetId for my animation made in my studio?

I export the animation and get the sharelink like this:

is the assetID is 02877821397 ?

1 Like

Yes. To format the asset ID as a content, prefix it with rbxassetid://. Your asset ID would be rbxassetid://02877821397.

2 Likes

Thank you so much Dandystan !

2 Likes

Thank u W0Qk and it works!

I have a further question . If I want the players communicate with those NPCs from his menu in a localscript . How can I do it ? Should I realize this by Remote Events and functions with another script in ReplicatedStorage ?

By communicating does that mean that you want an animation to play when a player communicates with it?

Yes. My game is about animal feeding.

when plays “feed” , my script should find the nearest animal NPC and play the corresponding animation.

You could find the nearest animal NPC with magnitude (there’s an example in the end of the page). When you find the one closest to you, use pathfinding to go to the animal. Then you play the animation.

1 Like

Hey I made a small closest NPC animation from what I believe you would want.
ClosestNPCAnimation.rbxl (31.6 KB)

1 Like

thank you so much buddy

I’ll try it later