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

Why does the animation not stop even though :Stop() is called?

Asked by 3 years ago

I am trying to make a trash can that you can push around and I'm stuck on the idle animation. This is the code:

isPushed = script.Parent.Pushing
proximity = script.Parent.BodyAttach.ProximityPrompt
idleAnimation = script.Parent.BodyAttach.Idle

function onTrigger(player)
    if isPushed.Value == false then
        local character = player.Character
        local humanoid = character:WaitForChild("Humanoid")
        local animator = humanoid:WaitForChild("Animator")
        local animateScript = character:WaitForChild("Animate")
        defaultAnim1 = animateScript.idle.Animation1.AnimationId
        defaultAnim2 = animateScript.idle.Animation2.AnimationId
        animateScript.idle.Animation1.AnimationId = idleAnimation.AnimationId
        animateScript.idle.Animation2.AnimationId = idleAnimation.AnimationId
        for i,track in pairs(animator:GetPlayingAnimationTracks()) do
            track:Stop(0)
        end
        local playerTorso = player.Character:WaitForChild("UpperTorso")
        M6D = Instance.new("Motor6D", playerTorso)
        M6D.Name = "TrashGrip"
        playerTorso.TrashGrip.Part0 = playerTorso
        playerTorso.TrashGrip.Part1 = script.Parent.BodyAttach
        proximity.Style = Enum.ProximityPromptStyle.Custom
        isPushed.Value = true
    else
        local character = player.Character
        local humanoid = character:WaitForChild("Humanoid")
        local animator = humanoid:WaitForChild("Animator")
        local animateScript = character:WaitForChild("Animate")
        animateScript.idle.Animation1.AnimationId = defaultAnim1
        animateScript.idle.Animation2.AnimationId = defaultAnim2
        for i,track in pairs(animator:GetPlayingAnimationTracks()) do
            track:Stop(0)
        end
        M6D:Destroy()
        M6D = nil
        proximity.Style = Enum.ProximityPromptStyle.Default
        isPushed.Value = false
    end
end

proximity.Triggered:Connect(function(player)
    onTrigger(player)
end)

As you can see there is a loop in both cases that should stop all animations that are playing. But it doesn't stop the idle animation and it only changes if the character moves and becomes idle again.

3 answers

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

I think in line 16 and 33, you put track:Stop(0)when it should be track:Stop() If you remove the zero then your script will be working fine.

0
I tried that, but it didn't change anything. Also :Stop() and :Stop(0) should theoretically do the same thing. vonotige 49 — 3y
Ad
Log in to vote
0
Answered by 3 years ago

Instead of stopping all animations on the player, I instead opted to play the idle animation until the player moves. This is done by checking if humanoid.MoveDirection is not equal to 0,0,0

Log in to vote
0
Answered by
1JBird1 64
3 years ago

instead of getting the animation tracks from the animator, get it from the humanoid instead.

so it would probably look like this:

for i,track in pairs(humanoid:GetPlayingAnimationTracks()) do
 if track.Animation.AnimationId == “rbxasset//insert_animation_id” then
  track:Stop()
 end
end

Answer this question