Hi. I’m trying to code an NPC system for a café style kind of game, where an NPC will spawn, and then walk towards the counter.
To make the walking actually look good I need the NPC to be playing an animation. The animations I’m using is the Roblox default idle, wave and walk animation.
The problem is that the animations won’t play at all. Previously they did play, but they were ran through a ServerScript at that point. We need the animations to play using a LocalScript, but it really doesn’t work.
We have two scripts, first one for the spawning of the actual NPC, which later triggers a Remote Event. I have established that there is nothing wrong with the actual firing of the RemoteEvent, therefore I won’t provide the Server script code here.
The local script code:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local AnimationsFolder = ReplicatedStorage:WaitForChild("Animations")
local Idle = AnimationsFolder.IdleAnim
local Walk = AnimationsFolder.WalkAnim
local Wave = AnimationsFolder.WaveAnim
local char = script.Parent
local Humanoid = char.Humanoid
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PlayNPCWave = ReplicatedStorage.PlayNPCWave
local animator = Instance.new("Animator")
animator.Parent = Humanoid
local IdleAnim = animator:LoadAnimation(Idle)
local WalkAnim = animator:LoadAnimation(Walk)
local WaveAnim = animator:LoadAnimation(Wave)
IdleAnim.Priority = Enum.AnimationPriority.Idle
WalkAnim.Priority = Enum.AnimationPriority.Movement
PlayNPCWave.OnClientEvent:Connect(function()
IdleAnim:Play()
end)
Me and my developing partner have tried a lot of different approaches to this, changing the Animator to an AnimationController, trying to load different animations etc…
Any help on this is much appreciated!
10 Likes
Try loading them on the Humanoid, I believe documentation states it will automatically create a Animator to load the animations on.
2 Likes
Still doesn’t work for me. No error messages or anything and the animation still doesn’t play.
1 Like
Local scripts have to be a descendant of the client’s player object or the client’s character. Because it is in its own object, the scripts will not work in this situation.
1 Like
But it still runs and reacts to the OnClientEvent. Is it just the animations that won’t work if it’s not a descendant of the player?
I was wrong, local scripts have to be a descendant of the client’s player object or the client’s character, but they also work in ReplicatedFirst. Your script is probably still active in ReplicatedFirst instead of inside the workspace.
Did you ensure the animation priority is high enough and if the remote events are actually being ran?
1 Like
There is already an animator inside the humanoid but you might have to use a waitforchild to get it.
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local AnimationsFolder = ReplicatedStorage:WaitForChild("Animations")
local Idle = AnimationsFolder.IdleAnim
local Walk = AnimationsFolder.WalkAnim
local Wave = AnimationsFolder.WaveAnim
local char = script.Parent
local Humanoid = char.Humanoid
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PlayNPCWave = ReplicatedStorage.PlayNPCWave
local animator = humanoid:WaitForChild("Animator")
local IdleAnim = animator:LoadAnimation(Idle)
local WalkAnim = animator:LoadAnimation(Walk)
local WaveAnim = animator:LoadAnimation(Wave)
IdleAnim.Priority = Enum.AnimationPriority.Idle
WalkAnim.Priority = Enum.AnimationPriority.Movement
PlayNPCWave.OnClientEvent:Connect(function()
IdleAnim:Play()
end)
1 Like
Yes, the priority is high enough, we also tried with the Action4 priority but no change in result. The remote event works
It still didn’t work unfortunately.
did any errors appear in the console?
Nope, there’s no error in the console at all
did you check to make sure that the line even runs with a print or something?
Yes we did try printing and it works!
Try running a loop on the animator to check if it reads that a animation actually plays. That will better help you.
1 Like
i’m unsure how to do that. what do you mean?
I really haven’t tried it, but its along the lines of this:
local Animator = script:FindFirstChildOfClass('Animator') --Change This
for __, AnimationTrack in pairs(Animator:GetPlayingAnimationTracks()) do
print(__, AnimationTrack)
end
There’s one random animation that pops up just when I use the LocalScript to play, “Animation1” or “Animation2”. But when I play it in a server script it’s just the IdleAnimation that is playing.
Most likely a replication issue? Try running the animation on the server just to see.
Yes, the animation runs on the server, just not on the client at all. The thing is that I need to be able to use the .IsPlaying but that doesn’t replicate in a server script.
1 Like