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

How to play an Animation when you press a key?

I've looked at the ROBLOX wiki, it usually just confuses me though. I've tried

if key == "q" then
runanim"Block"

I've looked at the other two, UserInputService and ContextActionService, but got more confused than i was at the start.

1 comment

0

I don't want to be mean but this is so bad, you really need to watch some tutorials. I recommend you watch AlvinBlox (YouTube channel) as he is very good at explaining.

2 answers

✓ Accepted answer

You obviously don't know how to run animations on a player, so I'll do that first.

Animating a character:

To animate a character you need an 'Animation' object. Put it in a LocalScript inside StarterPack (this will make the animation run when a player joins the game):

local anim = script.AnimationObject -- the animation I specified earlier

local p = game.Players.LocalPlayer -- the player
local char = p.Character -- their character
if not p.Character then -- checks to see if the character exists
	repeat wait() until p.Character -- if not, it waits until it exists
end
local human = char.Humanoid -- the character's humanoid (required to load an animation)

local loadAnim = human:LoadAnimation(anim) -- loads the animation into the player with the Animation object
loadAnim:Play() -- plays it

Hopefully my comments explained the script.

UserInputService (alternate version of KeyDown):

This is what you use to detect if a player has pressed a key, and it runs with events just like with KeyDown. Make the following script a LocalScript inside StartPack:

local serv = game:GetService("UserInputService")

serv.InputBegan:connect(function(key,gameProcessedEvent) -- when a player starts pressing a key, the second parameter is whether the game processed it (e.g, player is typing, so it wont process)
	print(key.keyCode)
end)

serv.InputEnded:connect(function(key) -- when a player stops pressing a key, optional for this context
	-- code
end)

To compile it all into your desired script:

The following should be put into a LocalScript inside StartPack:

local p = game.Players.LocalPlayer
local anim = script.Animation
local serv = game:GetService("UserInputService")
local char = p.Character -- their character
if not p.Character then -- checks to see if the character exists
	repeat wait() until p.Character -- if not, it waits until it exists
end
local human = char:WaitForChild("Humanoid") -- the character's humanoid (required to load an animation)

serv.InputBegan:connect(function(key,processed)
	if key.KeyCode == Enum.KeyCode.F and processed == true then -- change 'F' to your key, also checks to see if a player is typing (etc.) or not
		print("Starting animation!")
		local newAnim = human:LoadAnimation(anim)
		newAnim:Play()
	end
end)

Hope I helped :)

~TDP

1 comment

0

I have a question, what does the animation have to be called?

Well.

Don't use KeyDown, as it's deprecated.

Which means ROBLOX may remove it at any time.

I would recommend learning the basics of lua before going into keyDown and Inputbegan events.

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