I have this script to preload animations (and their events) by playing them and stopping them instantly on load. It works like a charm and prevents weird lag or stutter when playing an animation for the first time. However, now that I have over 32 animations to load, it gives me this error in my console. Nothing is actually broken and everything still loads fine but it’s just annoying and was wondering if anyone knew a better way to load animation events or prevent the error from throwing? Here is my module
local ContentProvider = game:GetService("ContentProvider")
local AnimationPreloader = {}
AnimationPreloader.__index = AnimationPreloader
function AnimationPreloader.new(character, animations)
local self = setmetatable({}, AnimationPreloader)
self.Character = character
self.Animator = character:WaitForChild("Humanoid"):WaitForChild("Animator")
self.Animations = {}
self.Tracks = {}
local assetsToLoad = {}
for _, anim in ipairs(animations) do
table.insert(assetsToLoad, anim)
end
ContentProvider:PreloadAsync(assetsToLoad)
for _, anim in ipairs(animations) do
local track = self.Animator:LoadAnimation(anim)
self.Animations[anim.Name] = anim
self.Tracks[anim.Name] = track
track:Play()
track:Stop()
end
return self
end
function AnimationPreloader:GetTrack(name)
return self.Tracks[name]
end
function AnimationPreloader:Play(name, fadeTime, loop)
local track = self.Tracks[name]
if track then
track:Play(fadeTime or 0)
if loop then
track.Looped = true
end
end
return track
end
return AnimationPreloader
AnimationTrack:Stop() does not stop the track immediately. Even if provided a fade time argument of 0, the track will still be considered to be playing until the animation system resolves during the next RunService.Stepped event.
You can avoid the warnings by not attempting to pre-play any animations when at the limit. Below is a code sample that does this asynchronously, although you can just throw the while ... do block into your animation track loader loop if you want to perform it synchronously. You may also wish to lower the yield threshold if there is a chance that external systems may attempt to play animations during the pre-play period.
local function PreplayAnimations(self)
assert(self.Thread == coroutine.running(), "Invalid thread")
for _, track in self.Tracks do
while #self.Animator:GetPlayingAnimationTracks() >= 32 do
task.wait()
end
track:Play()
track:Stop(0)
end
self.Thread = nil
end
function AnimationPreloader.new(character, animations)
local self = setmetatable({}, AnimationPreloader)
self.Character = character
self.Animator = character:WaitForChild("Humanoid"):WaitForChild("Animator")
self.Animations = {}
self.Tracks = {}
local assetsToLoad = {}
for _, anim in ipairs(animations) do
table.insert(assetsToLoad, anim)
end
ContentProvider:PreloadAsync(assetsToLoad)
for _, anim in ipairs(animations) do
local track = self.Animator:LoadAnimation(anim)
self.Animations[anim.Name] = anim
self.Tracks[anim.Name] = track
end
local thread = coroutine.create(PreplayAnimations)
self.Thread = thread
task.spawn(thread, self)
return self
end
function AnimationPreloader:Destroy()
local thread = self.Thread
if thread then
self.Thread = nil
if coroutine.status(thread) == "suspended" then
task.cancel(thread)
end
end
end
Thank you so much! This is why the error was thrown, and subsequently went away… @MysteriousVagabond 's method of loading animations is also better because it makes sure not to overload the animator.