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 ?
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.
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
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).
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 ?
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.