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

How to toggle a animation via KeyDown ?

Asked by 6 years ago

I'm trying to make an animation be toggled when I press 'c' on the keyboard. The character does the animation, but won't stop when I press C again...

The player is supposed to crouch and do it till I press C again:

wait(0.1)
local player = game.Players.LocalPlayer
local Mouse = player:GetMouse()
Anim = script.Animation
Toggle = false

if Toggle == false then
        Mouse.KeyDown:connect(function(key)
if key == "c" then
player.Character.Humanoid.WalkSpeed = 0
player.Character.Humanoid.JumpPower = 0
s = player.Character.Humanoid:LoadAnimation(Anim)
Toggle = true
s:Play()
end
        end)
        end


if Toggle == true then
        Mouse.KeyDown:connect(function(key)
if key == "c" then
player.Character.Humanoid.WalkSpeed = 16
player.Character.Humanoid.JumpPower = 50
s = player.Character.Humanoid:LoadAnimation(Anim)
Toggle = false
s:Stop()
end
        end)
        end

1 answer

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

Try this:

wait(0.1)
local player = game.Players.LocalPlayer
local Mouse = player:GetMouse()
Anim = script.Animation
local s = player.Character.Humanoid:LoadAnimation(Anim)
Toggle = false



Mouse.KeyDown:connect(function(key)
    if key == "c" then
        if Toggle == true then
            player.Character.Humanoid.WalkSpeed = 16
            player.Character.Humanoid.JumpPower = 50
            Toggle = false
            s:Stop()
        elseif Toggle == false then
            player.Character.Humanoid.WalkSpeed = 0
            player.Character.Humanoid.JumpPower = 0
            Toggle = true
            s:Play()
        end
    end
end)
0
Thanks, it works ! But could you tell me how to make a looping animation stop ? It all worked, but my character still stayied in the pose LordTechet 53 — 6y
0
Let's try that again, I edited it so the animation won't load every single time you press the c key. This time it only loads one and works around it, playing it and stopping it. kakikito123 4 — 6y
0
If you could accept my answer, that would be nice of you :) kakikito123 4 — 6y
Ad

Answer this question