local Animations = script.Animations:GetChildren() -- Gets the animations function stopAnimations() for i = 1,#Animations do Animations[i]:Stop() end end stopAnimations() -- Fires the function
Local script
located in StarterGui
Animations
folder located in the Local script
9 animation
s located in the folder
Error = Stop is not a valid member of Animation
Why isn't it stopping the animations? There of course are other and better ways to do this but I still want to understand what's wrong here.
To stop an animation, you have to stop it from the humanoid. You can't stop it from the actual animation object, as :Stop() is not a method.
if you only want to stop the animations inside the "Animations" directory, this script should work nicely:
local Animations = script.Animations:GetChildren() -- Gets the animations function stopAnimations() local playing = game.Players.LocalPlayer.Character.Humanoid:GetPlayingAnimationTracks() for _,Anim1 in pairs(playing) do for _,Anim2 in pairs(Animations) do if Anim1.Animation == Anim2 then Anim1:Stop() break end end end end stopAnimations() -- Fires the function
You are stopping the Animation
, which isn't playing. when you want to play an animation you must load it first with the LoadAnimation()
function, like this:
local Animation = --Your animation here local Humanoid = --The humanoid which will play the animation local AnimationTrack = Humanoid:LoadAnimation(Animation)
Then you play the track using the AnimationTrack:Play()
function, and if you want to stop the track you don't say Animation:Stop()
, you say AnimationTrack:Stop()
. By adding all tracks into an array you can stop all of them.
Here's how the code should look like:
local Humanoid = --The humanoid which will play the animation local AnimationTracks = Humanoid:GetPlayingAnimationTracks() -- All animation tracks local function StopTracks() for i = 1, #AnimationTracks do AnimationTracks[i]:Stop() end end --> Stop All the playing tracks StopTracks() --> Run the function