Preloading Animations

Hello! I’m noticing that the animations in my game play in a slight delay and I want to fix that my preloading every animation in the Workspace so that the game doesn’t struggle loading in the animations when they are supposed to play. Is there a short way to do that or do I have to write every single animation seperately like below?

local Humanoid = Character:WaitForChild("Humanoid")
local Animator = Humanoid:WaitForChild("Animator")
local Animation = Character.Animation

local LoadedAnimation = Animator:LoadAnimation(Animation)

local Humanoid2 = Character2:WaitForChild("Humanoid")
local Animator2 = Humanoid:WaitForChild("Animator")
local Animation2 = Character2.Animation

local LoadedAnimation2 = Animator:LoadAnimation(Animation2)

(This isn’t a script that’s in my game, the only way I know to load an animation is this way. This is only an example.)

3 Likes

Also I’m curious if there are any way to load a single animation properly (without it delaying it) by just using any kind of script.

Maybe you could try using ContentProvider:PreloadAsync. You need to pass in the Animation as an Instance though, and it also yields, so I recommend using task.spawn when calling it each time.

EDIT: You can pass an array of instances as well, that will also work

E.g.

local animations = {
  -- Many instances of class Animation with the ID already set
}


task.spawn(function()
    ContentProvider:PreloadAsync(animations)
end)

-- Do other stuff here at the same time...
4 Likes

Seems useful. Thanks for the suggestion I’ll look into it.

Also sorry I’m not really familiar with scripting so I don’t really know what “yield” means. Can you explain it to me because I didn’t really understand why I would need the task.spawn.

Edit: I’m looking at the page you’ve mentioned and it doesn’t use that command or whatever. It seems that just writing the animations down as variables and then adding them to the PreloadAsync works?

I can see it there just fine.

Ah, well coroutines are a whole 'nother can of worms but to put it simply.

Each coroutine is like a string of code, it can pause at any time and cause another string of code to continue. Only ever can one string of code be running at one time. These “strings of code” are called threads or coroutines.

When a coroutine resumes another one, it pauses and waits for that other coroutine to finish or yield. When the second coroutine yields, it pauses its execution and returns a value to the original coroutine that started it. It then waits until it is resumed again.

Example:

local firstThread = coroutine.create(function()
    -- [1]
    print("I am here in first thread. I will resume secondThread now, and wait for it to finish")
    -- [GO TO 2]
    local status, num = coroutine.resume(secondThread)
    -- [3]
    print(`Second thread finished what it is doing and returned the number {num}. But we can resume it again`)
  
    -- We send "abcdef" to secondThread
    -- [GO TO 4]
    coroutine.resume(secondThread, "abcdef")
    -- [5]
    local status2 = coroutine.status(secondThread)
    print(`The secondThread is now {status2}`)
    -- [END OF SCRIPT. FINISH]
end)

local secondThread = coroutine.create(function()
    -- [2]
    print("I am now in second thread, I will now perform an intensive task")

    ... -- Something that takes a long time to do

    print("I am done with it now, so I will yield and return 123 back to the firstThread")
    -- [GO TO 3]
    local msg = coroutine.yield(123) -- This thread will now pause and resume firstThread
    -- [4]
    print(`I am now back in secondThread and I received {msg} from firstThread`)
    return -- That's it, we're done.
    -- [END OF SCRIPT. GO TO 5]
end)

coroutine.resume(firstThread)

Follow the [#] comments to see how the execution flows between each coroutine.

3 Likes

Oh I see. Thanks for explaining. I’ve now wrote down all the variables into the PreloadAsync brackets. The variables are the Animation instances that should work right?

Edit: Also one final question if that’s alright for you, how can I check if an instance is a child of Workspace? Basically my game creates a few copies of the Player’s character and the animations are inside them. Though there is a process to that and the character models get inside the Workspace later on. I want the script to wait at first and after the character models are sent into Workspace, I want it to do it’s job.

Maybe a repeat until command could work though I’m not sure how I would make it work.

Why not store this information in a table? I don’t reqlly know how you did it so I cant help much more but you could store thr copies as keys and true as their values so you can tell if one is a real or fake

local clonelookup = {}
clonelookup[clone] = true

isclone = clonelookup[clone] or false
print(`Is {clone} a clone? {isclone}`)

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