Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to toggle a animation via KeyDown ?

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

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)

3 comments

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

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.

0

If you could accept my answer, that would be nice of you :)

This discussion is preserved as it was. New answers, comments, and votes are closed.