Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Why isn't this for loop working and stopping animations?

Asked by
MiguRB 60
5 years ago
Edited 5 years ago
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 animations 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.

2 answers

Log in to vote
1
Answered by 5 years ago

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

0
Thank you for answering! :) MiguRB 60 — 5y
0
No problem :) Internal_1 344 — 5y
Ad
Log in to vote
0
Answered by
Vik954 48
5 years ago
Edited 5 years ago

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
1
Thank you both for your explained answers! :) MiguRB 60 — 5y

Answer this question