Animations load too long

So I have a local script called AnimationHandler that obviously handles the animations. And the problem is when a player just spawned, it takes a few seconds to load all the animations into the humanoid and thats really annoying for some players. I tried removing the repeat until, but then it just doesn’t load the animations

repeat task.wait()
	for i, v in pairs(Tool.Animations:GetDescendants()) do
		if v:IsA("Animation") and v.Name == "AttackRightShieldless" then
			AttackRightShieldless = animator:LoadAnimation(v)
			table.insert(allAnims, AttackRightShieldless)
			table.insert(possibleAttackAnimations, AttackRightShieldless)
			AttackRightShieldless:SetAttribute("Direction", "Right")
		elseif v:IsA("Animation") and v.Name == "AttackLeftShieldless" then
			AttackLeftShieldless = animator:LoadAnimation(v) 
			table.insert(allAnims, AttackLeftShieldless)
			table.insert(possibleAttackAnimations, AttackLeftShieldless)
			AttackLeftShieldless:SetAttribute("Direction", "Left")
		elseif v:IsA("Animation") and v.Name == "AttackOverheadShieldless" then
			AttackOverheadShieldless = animator:LoadAnimation(v) 
			table.insert(allAnims, AttackOverheadShieldless)
			table.insert(possibleAttackAnimations, AttackOverheadShieldless)
			AttackOverheadShieldless:SetAttribute("Direction", "Up")
		elseif v:IsA("Animation") and v.Name == "AttackStabShieldless" then
			AttackStabShieldless = animator:LoadAnimation(v) 
			table.insert(allAnims, AttackStabShieldless)
			table.insert(possibleAttackAnimations, AttackStabShieldless)
			AttackStabShieldless:SetAttribute("Direction", "Bottom")
		elseif v:IsA("Animation") and v.Name == "Equip" then
			Equip = animator:LoadAnimation(v)
			table.insert(allAnims, Equip)
		elseif v:IsA("Animation") and v.Name == "Block" then
			Block = animator:LoadAnimation(v)
			table.insert(allAnims, Block)
		elseif v:IsA("Animation") and v.Name == "Idle" then
			Idle = animator:LoadAnimation(v)
			table.insert(allAnims, Idle)
		end
	end
until workspace:IsAncestorOf(character)

I guess there should be a different method to handle the animations loading, but I have no idea of how would I make it

1 Like

Try Preloading the Animations with ContentProvider

local CProvider = game:GetService("ContentProvider")

for _,i in pairs(game:GetDescendants()) do
   if i:IsA("Animation") then
      CProvider:PreloadAsync(i)
   end
end)

4 Likes

This is correct but if it doesn’t work for you, although I don’t think so, change the cycle

1 Like

I’ve tried it, and it outputs and error
Unable to cast to Array

1 Like

Try putting a table inside

CProvider:PreloadAsync({i})
1 Like

Alright now it works, but how do I load it into a humanoid now? Do I put it into the function itself like in my original script? I have never used ContentProvider :frowning:

1 Like

Put it in ServerScriptService

eijfmew

1 Like

how do i use the preloaded anims

1 Like

game:GetDescendants is way too much, instead, you should preload the descendants folder holding all your animations.

Its only preloading the Animations, shouldn’t be much

You’re actually supposed to preload from the client, so make put a plain local script in StarterGui and preload in there.

You use them as if they’re regular animations. They don’t change at all.

1 Like

Just place it in StarterPlayerScripts then if you want to use the Client :confused:

2 Likes

I’ll try it all when I get back home, thank you all

StarterGui and StarterPlayerScripts are the same thing, there’s no difference.

So it’s on StarterCharacterScripts and NOT StarterPlayerScripts.

Please, StarterGui and StarterPlayerScripts are the exact same thing, it’s like WalkSpeed, JumpPower, Health, MaxHealth, UseJumpPower and more !

So, I’d rather do StarterCharacterScripts instead of, what you’ve said.

Its Better to put it in StarterPlayerScripts since it only runs once as the Player Joins for the first time

1 Like

Yes, use StarterPlayerScripts, not StarterGui or StarterCharacterScripts. StarterGui or StarterCharacterScripts will clear itself, so scripts will rerun and load again, adding onto unnecessary performance. StarterPlayerScripts only run once and don’t rerun or load again.

1 Like

Content Provider simply replicates those items first.

You can load an animation by doing Humanoid:LoadAnimation(ANIMATION_HERE). The humanoid being the desired humanoid. After you load the animation you can do :Play() to start it.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.