Preloading Animations

I’m trying to preload some Animation objects from existing ROBLOX animation packs to play locally on an NPC. I searched the devforum and found a post from August 2015 about how PreloadAsync didn’t work for animation instances. Is this still a problem?

image

Here’s the code I guess.

local loadedAnims = {}
local animPacks = {ninja = 658839027, cartoony = 837018588}

for animPackName, animPackId in pairs(animPacks) do
	loadedAnims[animPackId] = {}
	for _,animAssetId in pairs(game:GetService("AssetService"):GetAssetIdsForPackage(animPackId)) do
		local anim = Instance.new("Animation")
		anim.AnimationId = 'rbxassetid://'..animAssetId
		loadedAnims[animPackId][#loadedAnims[animPackId] + 1] = anim --[animAssetId] = humanoid:LoadAnimation(anim)
	end
end

for _, animPackId in pairs(loadedAnims) do
	game:GetService("ContentProvider"):PreloadAsync(animPackId)
end
1 Like

I might be completely wrong but I’m pretty sure PreloadAsync only works on the server.

1 Like

I thought preloading was for the client?

1 Like

Yes, preloading has been made for clients.

The first item on the error list you try to preload is a catalog item and not an animation.
This catalog item can be used to load the specific animations from it using InsertService:LoadAsset, just like clothing or accessories. It’s not an asset type which can be preloaded using the ContentProvider.

Loading such a catalog animation item using InsertService gives you a model with a folder containing StringValue(s) containing the attached animation(s), as they can be seen as children of the Animate LocalScript inside of a character. The animations inside have the specific AnmationIds used for the character animations.

2 Likes

Wrath says “I am an idiot, I am opening studio right now!”

1 Like

Thanks, that code was from my testing area and you helped me realize my error. I probably shouldn’t have posted this at like 1 am. Say I preload one Animation object with an asset id. I don’t preload another Animation object but it has the same asset id. Is the second animation not really preloaded since I didn’t call PreloadAsync on it even though it has the same asset id as the one I did preload?

1 Like

When an asset is attempted to be loaded, Roblox will first check if the asset has been downloaded before and load it from the cache, or download and save it to the cache if it hasn’t been loaded before. The cache uses the given asset id as identification.

Preloading an animation instance will therewith, if not already cached, create a cache file and download the asset to it. Loading the same asset id on another instance will then automatically pull it from the cache. You don’t have to preload it again.

Extra fact: This is the reason why images of your own avatar within a game take a while to update after you changed your avatar appearance, provided you already played the game shortly before you edited your avatar.

1 Like