Weird animation behavior

I’ve spent hours scratching my head at why my animations weren’t playing when I run loadedAnimation:Play().

It turns out, Animations do weird stuff if the animation playing it is in a global function.

I had a separate script:

[spoiler][code]local animations = {}
local loadedAnimations = {}

for _,v in pairs(game.ReplicatedStorage.Animations:GetChildren()) do
loadedAnimations[v.Name] = game.Players.LocalPlayer.Character:WaitForChild(“Humanoid”):LoadAnimation(v)

function _G.AddAnimation(anim, tween, weight, speed)
if animations[anim] then
local animation = loadedAnimations[anim]
animations[anim] = animation
animation:Play(tween, weight, speed)

function _G.RemoveAnimation(anim, tween)
local i = 1
for k,v in pairs(animations) do
if k == anim or anim == “All” then
coroutine.resume(coroutine.create(function() – Just for extra stability.
animations[k] = nil
if anim ~= “All” then break end

And when I run _G.LoadAnimation() in a separate script, it doesn’t play.

However, if I don’t make the functions global, and add the functions to each of my LocalScripts, it will all work perfectly fine.

There’s a few little quirks like this with animations…
I wish I had time to make a list of them

They also aren’t working in modules, which is extremely annoying.

If you ask me, that makes total sense that they don’t work with _G or a ModuleScript. Modules are meant to work both server sided and on the client. Where as, for the functions you assigned globally, my guess is that you did this through a server sided script. It’ll only work through use of a LocalScript.

Hmm, so it seems I have now found a good solution.

I’m using a module that returns a metatable, and in its properties, it stores the loaded animations, and through the metatable, I can use global animations.

Before, I was storing the loaded animations in a local table in the un-returned part of the module.

As for using _G, the global functions played the animations from a local table.
I guess this makes more sense now.