How to stop a track that is created locally?

So, recently i’ve been doing some animations to use and i wanted to create some kind of idle for my item.
I create the function that animates inside a module, and the track, inside the function. I can play it, but everytime i try to stop it, it won’t work, as everytime i run the animation, a new track is created.
I can’t find a way to create the track outside the function.
I can send a model of what i did.

This is a pretty vague problem and you’ll definitely need to provide some code on what’s going on.

1 Like
-- ModuleScript

function Animation()
  local track = character.Humanoid:LoadAnimation(theanimation)
  track:Play()
end

function getAnimation()
  -- a way to get the animation the character is doing, as the track is created in a function, i can't get the same track
end

function Eqquiped()
  -- this runs when i equip the part (with a button) and this also runs the Animation()
end

function Unequipped()
 -- this is where it was supposed to stop the track but i can't find a way to stop it,
 -- but i can't find a way to stop it, as the track itself gets created each time i run Animation()
end

Just make the variable track local to the module and not the Animation function. That way all functions inside the module can modify the variable.

local track
function Animation()
  track= character.Humanoid:LoadAnimation(theanimation)
  track:Play()
end

function getAnimation()
  -- a way to get the animation the character is doing, as the track is created in a function, i can't get the same track
end

function Eqquiped()
  -- this runs when i equip the part (with a button) and this also runs the Animation()
end

function Unequipped()
     if track then track:Stop() end -- this is where it was supposed to stop the track but i can't find a way to stop it,
 -- but i can't find a way to stop it, as the track itself gets created each time i run Animation()
end