I have a function foo
, when triggered plays an animation on a humanoid, given arguments are an Animation
object and a humanoid
object, How do I know if that animation is already loaded on that humanoid and, if it is loaded then just play that?
It should be pretty simple! All you have to do is make a nil variable, and in function foo
, detect if it isn’t nil, which means it should have an animation loaded onto it. Here’s some sample code if it helps.
local anim -- this is the variable you set to the loaded animation
local function foo()
if anim ~= nil then -- check to see if it has a loaded animation
anim:Play()
end
end
I recommend you making a getAnim function
local loadedAnimations = {}
local function getAnim(animObject)
if loadedAnimations[animObject] then
return loadedAnimations[animObject]
end
loadedAnimations[animObject] = animator:LoadAnimation(animObject)
return loadedAnimations[animObject]
end)
now you can just do getAnim(animationObject):Play() and not worry about it overloading
Not this, this has a flaw, You are indexing with animObject, but what if I load this animation on animator A and try to get on animator B? This is going to cause issues. Is there anyway to just get all loaded animation tracks on a humanoid or animator?
if you want to use this for multiple animators then do this:
local loadedAnimations = {}
local function getAnim(animator, animObject)
loadedAnimations[animator] = loadedAnimations[animator] or {}
if loadedAnimations[animator][animObject] then
return loadedAnimations[animator][animObject]
end
loadedAnimations[animator][animObject] = animator:LoadAnimation(animObject)
return loadedAnimations[animator][animObject]
end)
Yep It works, I’ve also learned that if a track is already loaded, it won’t load again if used LoadAnimation
more than once, and it ain’t gonna give out memory leaks.
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.