How to load ALL animations of a folder once

so im making a script that loads all animations at once, the problem is:

it works! but randomly.

let me explain better

local function LoadAnimations()
	for i,v in pairs(animFolders) do
		for i,anim in pairs(v:GetChildren()) do
			local track = hum.Animator:LoadAnimation(anim)
			print(anim)
			track:Play()
			os.clock()
		end
	end
end
	
LoadAnimations()

this scripts run the code without error, however, if my folder has 5 animations, the script prints (Animation 3, Animation 1, Animation 5), meaning that the script loads randomly the animations AND without loading all of them. i tried to run this a second time, printing (Animation 1, Animation 4, Animation 2, Animation 5)

I tried to fix this by using hum.Animator:LoadAnimation() and it just loads more animations more often, instead of hum:LoadAnimation()

before you say " use content Provider", im already using it but for some reasons it doesnt load Hum.Animation:LoadAnimation(anim).Length

1 Like

Use ipairs instead.

1 Like

If the length is 0 or nil it means the item has not fully loaded in yet. It’s one of the ways you can track waiting for the request to get loaded in before moving forward.

2 Likes

tried but nothing changed.

I need more characters to send this comment

this sounds good let me try it

I believe its most likely due to LoadAnimation not yielding. Each animation probably takes a different amount of time to load so you could try something like waiting for all to be loaded, then playing them.

Edit: lol just realised I just said what @TheCraneStyle did but longer (I get +10 points for stating the reason why it loads differently though!).

1 Like

yeah that was the problem, i wanted to put your comment as the solution but too late :C

1 Like
--[[
I have no idea why you want to do this this way but there is a better overall way to handle this
Notes:

Yes this is going to be a few more lines of code, get over it, easier to read and gets your logic a bit
clearer
]]

local cacheLoadedAnimations = {}

-- Old way
--local function LoadAnimations()
--	for i,v in pairs(animFolders) do
--		for i,anim in pairs(v:GetChildren()) do
--			local track = hum.Animator:LoadAnimation(anim)
--			print(anim)
--			track:Play()
--			os.clock()
--		end
--	end
--end

--[[
Filter functions are boss 
]]
local function filterAnims(instanceTable)
	local newTable = {}
	for i = 1, #instanceTable do 
		if instanceTable[i]:IsA("Animation") then 
			table.insert(newTable, instanceTable[i])
		else 
			continue
		end
	end
	return newTable
end

--[[
We sure like it warm and ready to use when we need it.
This is where we ensure each animation that we absolutly can't live without 
is loaded
]]
local function warmUpAnimation(AnimationInstance)
	-- You need to fill in humanoid ect i ant do ing all of it for you =-P
	local track = Humanoid.Animator:LoadAnimation(AnimationInstance)
	
	if track.Length == 0 then
		repeat
			task.wait()	
		until track.Length > 0 
	end
	
	track:Play()
	
	--- This is wehre i suggest you then name and or cache your animations
	--- that you have loaded 
end

--[[
Loading Animations, dunno why but we doing it
]]
local function LoadAnimations()
	local AllAnimations = filterAnims(AnimHolder:GetGetDescendants())
	
	for i = 1, #AllAnimations do 
		warmUpAnimation(AllAnimations[i])
	end
	
end

LoadAnimations()
1 Like

haha thanks, if your wondering why im loading all the animations at once, is because i made a sword that use Animation events to enable and disable the hitbox, while using Animation.Length

however, the Animation.Length was 0 even if the animation was playing without delay. so after many hours i found the problem and this is the only way i got this to work.

anyway, maybe im not the best scripter but i surely got useful informations with your script. thank you

1 Like

Shrugs, I have different standards on what makes a good scripter. Plus I am the kind of person that just holds people to high standards regardless because how do you get better?

That being said, from personal experience, I highly suggest not using animation events and instead processing or knowing your events of your animation ahead of time or processing named areas and getting their time value.

Then when your event happens, queue up timed events to go off in sequence. This is considerably more reliable, and it makes it considerably easier to manage lag disparity. It is a bit more taxing to set up though, but the consistency is worth it.

When I say named areas, you can actually pull all the keyframes of an animation and you can parse out Named Events and get their times. You use that to queue up time. Though you do load the animations ahead of time instead of loading the whole animation you just load up the keyframe information. This makes you not animation dependant, and thus even if for some reason your animation fails to play it will still work and sync with the time you intend it too.

1 Like

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