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

Keydown linked to animations?

How do I make a Keydown execute a certain animation such as a dodge to the left of some sort?

2 comments

1

Scripting Helpers is not a request site. I would suggest going to the roblox wiki to learn about events and the connect function.

0

I actually have quite a lot of experience in keydown linking to animations. Currently i dont have access to my computer, but in a few hours ill be more than happy to help you :)

1 answer

Before you make the code for the animation, you have to create the animation itself in the animation editor made by roblox. Here is a link: https://www.roblox.com/Animation-Editor-item?id=144373835 Download that plug-in in roblox studio then make your animation. When you make your animation, make sure to name your first and last keyframe (Definitely your last keyframeAfter because this is what will actually be in the code) that, you need to first import your animation in the code, then you have to import your animation via your animation id, then you will be able to actually bind it to a key. Here is a link to everything you need to know about animations: http://wiki.roblox.com/index.php?title=Animations At the bottom of the screen, you will see some code that is specifically for animations. That is the code that you want to manipulate. The code will be:

-- Import animation
local animation = Instance.new("Animation")
animation.AnimationId = "http://www.roblox.com/Asset?ID=144911345" -- Replace the numbers with the numbers from your animation.  It should be in your animation Id.
 
-- Local variables
local animTrack = nil
local canPlay = true
 
function playShockAnim(Source) -- replace this with the key that was pressed
	if canPlay then
		local player = game.Players.LocalPlayer.Character
		canPlay = false
		animTrack = player.Humanoid:LoadAnimation(animation) -- Load animation into Humanoid
		animTrack.KeyframeReached:connect(function(keyframeName) -- Bind function to KeyframeReached event
			if keyframeName == "ElectrocuteEnd" then -- replace this with the name of your last keyframe name.
				canPlay = true
			end
		end)	
		animTrack:Play() -- Start the animation
	end
end

In this code, what you really want to take out is: function playShockAnim(Source)

The code that I have is put in a local script in a tool, so what I have is:

local tool = script.Parent
tool.Equipped:connect(function(mouse)
mouse.KeyDown:connect(function(key)
if
	key == "q" then

This is what replaces the function playShockAnim(Source) This is put in a local script in a tool. The final code would look something like this:

local animation = Instance.new("Animation")
animation.AnimationId = "http://www.roblox.com/Asset?ID=144911345" -- Replace the numbers with the numbers from your animation.  It should be in your animation Id.

local canplay = false
local tool = script.Parent
tool.Equipped:connect(function(mouse)
mouse.KeyDown:connect(function(key)
if
	key == "q" and canplay then
	local player = game.Players.LocalPlayer.Character
		canPlay = false
		animTrack = player.Humanoid:LoadAnimation(animation) -- Load animation into Humanoid
		animTrack.KeyframeReached:connect(function(keyframeName) -- Bind function to KeyframeReached event
			if keyframeName == "ElectrocuteEnd" then -- replace this with the name of your last keyframe name.
				canPlay = true
			end
		end)	
		animTrack:Play() -- Start the animation
	end
end
	

That should be all, but like RedCombee said, this is not a request site, you should look at the links i have supported and try to make a little code of your own then show it and then we can help you instead of giving it to you. Anyways, that should be all, hopefully you can get your animation into whatever game you're making. Cheers :)

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