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

How do I write a script to stop an animation that was previously playing?

Asked by 8 years ago

I want the script to: First: detect if the animation is actually playing at the moment Second: if it is, stop it. Third: if you move your character (press w,a,s,d) the animation stops and your character goes back to normal (which is a part I feel like I'm complicating and feel like there's a way simpler way)

Here is my script to give you awesome people an idea of what I'm talking about :)

player = game.Players.LocalPlayer
hum = player.Character.Humanoid

mouse = player:GetMouse()

animation1 = script:WaitForChild("One")
animation2 = script:WaitForChild("Two")
animation3 = script:WaitForChild("Three")
animation4 = script:WaitForChild("Four")

function endOne()
    local oneTrack = hum:LoadAnimation(animation1)
    oneTrack:Stop()
    print('animation1 successfully stopped!')
end

function endTwo()
    local twoTrack = hum:LoadAnimation(animation2)
    twoTrack:Stop()
    print('animation2 successfully stopped!')
end

function endThree()
    local threeTrack = hum:LoadAnimation(animation3)
    threeTrack:Stop()
    print('animation3 successfully stopped!')
end

function endFour()
    local fourTrack = hum:LoadAnimation(animation4)
    fourTrack:Stop()
    print('animation4 successfully stopped!')
end

enabled = true

mouse.KeyDown:connect(function(key)
    if key == "e" then -- Animation 1
        if enabled then
            enabled = false

            endTwo()
            endThree()
            endFour()
            wait()

            local animationTrack = hum:LoadAnimation(animation1)
            animationTrack:Play()

            wait(2)
            enabled = true
        end
    end
end)

mouse.KeyDown:connect(function(key)
    if key == "r" then -- Animation 2
        if enabled then
            enabled = false 

            endOne()
            endThree()
            endFour()
            wait()

            local animationTrack = hum:LoadAnimation(animation2)
            animationTrack:Play()

            wait(2)
            enabled = true
        end
    end
end)

mouse.KeyDown:connect(function(key)
    if key == "t" then -- Animation 3
        if enabled then
            enabled = false

            endOne()
            endTwo()
            endFour()
            wait()

            local animationTrack = hum:LoadAnimation(animation3)
            animationTrack:Play()

            wait(2)
            enabled = true
        end
    end
end)

mouse.KeyDown:connect(function(key)
    if key == "y" then -- Animation 4
        if enabled then

            local animationTrack = hum:LoadAnimation(animation4)
            animationTrack:Play()

            wait(2)
            enabled = true
        end
    end
end)

mouse.KeyDown:connect(function(key)
    if key == "w" or "a" or "s" or "d"  then -- Stop all animations / back to normal
        if enabled then
            endOne()
        end
    end
end)

Thanks :)

0
The children "One", "Two", "Three", and "Four" are my animations (just to let you guys know) :D shasta342 37 — 8y

Answer this question