How do I handle animations for a large number of NPCs?

I’m creating a game that has a lot of NPCs moving around at once, and animations right now are the biggest thing giving me trouble.

I can get them to animate just fine, it’s that animation scripts are really, REALLY demanding and often hog resources, making the server lag.

Fair enough, you say, just move it to the client! So that’s what I tried next, making the client handle all of the NPCs animations instead. And that (mostly) fixed server lag issues. However, once ~25+ NPCs were in game, my frames started dropping like crazy. (And I’m not running cheap hardware, mind you.)

So. I’m wondering what’s the best way to go about making a lot of NPCs animate nicely without hogging resources on either end. Any suggestions?

1 Like

You must be doing something wrong, for my game all player animations and NPC animations are handled locally. Tried it with 75 players and 100 NPCs and it worked fine.

All I do is just loop through all players and play animations based on what they’re doing 15 times a second, so for example if their velocity is higher than 1 on x or z I play the running animation, if their velocity on y is higher than 1 I play the jumping animation.

2 Likes

Where did you have the script acting from? What it in PlayerGui, StarterPlayerScripts, or somewhere else?

1 Like

Well it was in a module script function which was called from a local script in PlayerGui, but you could just put it in a normal local script. I like to keep my things organised.

1 Like

I agree with @vsnry because roblox has introduced the 100 player limit and they all use animations i dont think its a roblox problem or hardware you may want to try what vsnry suggested

1 Like

Would you do something like

while wait(1/15) do
for _, child in pairs(game.Workspace.NPCs:GetChildren()) do
–Animation Stuff
end
end

Yep, pretty much exactly what I did except I used a standard for loop as they are much faster.

local children = workspace:GetChildren()
for i = 1, #children do
    print(i, children[i].Name)
end
4 Likes