I am using custom animations. I animate them on server by putting the animation in the script and the Humanoid:LoadAnimation and Animation:Play. Seems to work fine.
But now I want to move the animating function to the client.
Could someone talk me through the steps on how to do this? Here is what I got.
Server controls movement. When it moves fires RE to tell client to either play run animation or idle (NPCs only have two animation kinds).
Loaded animations into local script stored in StarterPlayerScripts.
When an NPC is added to workspace NPC folder, then using Folder.ChildAdded:Connect(function(NPC) to get the new NPC.
Using NPC:FindFirstChild(“Humanoid”) to get humanoid part. But this is failing and I am getting the error that the NPC does not have a Humanoid. Why? It does have one.
Then local script uploads animations to humanoid of NPC.
REs on local listening and then play the animations.
Is this the right approach? It isn’t working at all at the moment. But is the concept right? Should I be doing this in another way?
I know that Humanoid:LoadAnimation is depreciated. So I can do this with Animator:LoadAnimation.
Current problem is that the ChildAdded is not getting the model, but only the name. I can have server send NPC id number and then loop through all items in NPC folder, I guess.
Well in my case I use a LocalScript that requires modules inside of each NPC that contains the script to animate them. There is no need for remotes, you can just use a LocalScript (at least if it’s just for movement animations).
The game has lots of NPCs. Each player can have up to 12 pets that follow him around and attack targets. I am worried about lag if everything is on the server.
Localscripts have permissions to play animations to the server without the need for events of any sort, in fact, i find it alot smoother to use animations on the client. You can just copy your current animation script to a localscript
It’s about deciding to sacrifice some server resources (aka ping) or framerate. Usually it’s better to use the client if there’s a lot of NPCs as animating them on the server is quite expensive with large numbers.
You can just put the localscript in “StarterPlayerScripts” and basically have it find the NPC in workspace and animate it using the same stuff you’d use for a normal script
But how would the local script know if the NPC is moving (for walking animation) or not? Don’t I need a remote to tell the client whether the NPC is moving or not?
AFAIK that is only true for animations that are parented to the client’s character or rigs that the client has network ownership over. Clients can’t normally replicate animations to other clients arbitrarily.
Ah i see you’re trying to replace its walking animations, thats also pretty simple, just copy the “Animate” script from yourself and head back into the editor and replace the anim IDs with the custom ones, and have a SERVERSCRIPT clone the custom animation script to the NPC
Make sure you have the scripts name as “Animate” and disable it before you clone it, and enable the clone.
Sure, but that plays everything on the server. I have no trouble animating the NPCs with a server script. I just can’t get them to animate on the client.
Does using one server script for all NPCs save power over using a script in each NPC?
You should only really animate NPCs locally if the animation should only be seen by the local player and not others. I wouldn’t really recommend wasting time on trying to reinvent the wheel and creating animation replication manually. If you do it on the server, you allow Roblox and the engine to handle all the animation replication for you, which they intend to optimize better over time.
Okay, but then I would be running all of my NPCs server side. Wouldn’t there be a lag problem if there were lots of NPCs (say 200) all using custom animations?
Yeah by cloning the custom animate script with a LOCALSCRIPT, the custom animation will ONLY be visible on the clientside therefore the custom animations will only play on the client