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

Why wont this animation stop playing once Z is pressed a second time?

Asked by 5 years ago
Edited 5 years ago

I am currently working on a crawling script for my game. I have a LocalScript in StarterPack with an Animation in the LocalScript. In my code, it starts playing the animation when Z is pressed once, but when it is pressed a second time it should stop but doesn't. In the output, it doesn't print ("Z was pressed again") after it's pressed a second time. The animation is looped, is in R15, and the priority is action. Here is what I currently have.

function KeyPress(input, gameProcessed)
    local pressed = 0
    local animation 
    game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script.Animation)
    if input.KeyCode == Enum.KeyCode.Z then
        pressed = 1
        print("Z was pressed")
        animation:Play()
    else
        if input.KeyCode == Enum.KeyCode.Z and pressed == 1 then
            pressed = 0
            animation:Stop()
            print("Z was pressed again")
        end
    end
end

game:GetService("UserInputService").InputBegan:Connect(KeyPress)

If anybody knows why this is not working it would be greatly appreciated if you answered.

0
The issue I see with your script is that you are declaring a local variable `pressed` each time the button is pressed. It is being assigned to 0 each time. It should be moved outside of the function. User#24403 69 — 5y
0
Now it prints ("Z was pressed again"), but still doesn't stop the animation. KiirbyKeeper 2 — 5y

2 answers

Log in to vote
0
Answered by 5 years ago

It seems like you want to implement a toggle, where a keypress would toggle the animation on or off. Here's how that should work:

local toggle = true -- toggle starts as turned on

function onKeyPress()
    if toggle == true then  -- if toggle is on
        toggle = false      -- turn it off
    else
        toggle = true       -- if toggle is off, turn it on
    end
end

To apply that to your animation, the toggle is whether or not the animation is playing, and the keypress is the Z key. So you'd implement it like so:

  1. When the Z key is pressed, check if the animation is playing.
  2. If the animation is playing, stop it. If it's not playing, play it.

And here's how your code looks like, with that implementation:

function KeyPress(input, gameProcessed)
    local animation = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script.Animation)
    if input.KeyCode == Enum.KeyCode.Z then -- Z key was pressed
        if animation.IsPlaying then
        animation:Play()
    else
        animation:Stop()
    end
    end
end

game:GetService("UserInputService").InputBegan:Connect(KeyPress)

Hope that helps!

Ad
Log in to vote
0
Answered by 5 years ago
function KeyPress(input, gameProcessed)
    local pressed = 0
    local animation 
    local animation_name = script.Animation.Name
    game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script.Animation)
    if input.KeyCode == Enum.KeyCode.Z then
        pressed = 1
        print("Z was pressed")
        animation:Play()
    else
        if input.KeyCode == Enum.KeyCode.Z and pressed == 1 then
            pressed = 0
            for i,v in pairs(game.Players.LocalPlayer.Character.Humanoid:GetPlayingAnimationTracks()) do
                if v.Name == animation_name then
                    v:Stop()
                end
            end
            print("Z was pressed again")
        end
    end
end

game:GetService("UserInputService").InputBegan:Connect(KeyPress)

u can use get playing animation tracks to check what animations humanoid is playing then u can easily stop any animation you want. I have no idea what the animation name is so i used "script.Animation.Name" you can correct it on the line : "if v.Name == animation_name then" to : if v.Name == youranimationnamehere then

0
You have to load in your animation in the animator plugin and go to settings at the left top and take off loop [set it to false] hereafterdeath -5 — 5y

Answer this question