What is the best way to preload tool animation in server script?

Ok, so I want to preload tool animation. Because loading animation took long time.
I can’t do it on local script in tool since local script in tool is yielded forever.
I can’t do it on server script cause you know why. We can only access player object when we send it trough Remote Event.

So, Is there any solution?

u can make a cache for that, click here for explanation

to keep stuff organized you can make a folder in ReplicatedStorage and call it Animations

local cache = {}

local animations = game:GetService("ReplicatedStorage"):WaitForChild("Animations")
--[[
the line above will not work if
there's no folder called Animations
(you can change the name, but don't forget
to change the name in the folder/script too!
]]

--this will load the animations objects in the folder to the cache once
for i,v in pairs(animations:GetChildren()) do
local loadedAnim = humanoid:LoadAnimation(v)
cache[v.Name] = loadedAnim
end

now if you want to use the animation you can just do this:

cache["AnimationName"]:Play() 
--example for an attack
cache["Attack"]:Play()
wait(2)
cache["Attack"]:Stop()

animation name is case sensitive and needs to match one of the animations’ names in the Animations folder in ReplicatedStorage

1 Like

Yeah, but it’s locally in localscript. I make a tool that has localscript. Did you know that tool script is yielded forever?

what do you mean yielded forever?

I just found the solution. We need to preload it with ContentProvide:PreloadAsync()

And supply the table full of the id of the animation.

You could also do that with sound and decal