Im relatively new to scripting, So I need some help with this. I have the run animation ready.
Help making a script that starts an animation when you press a key?
3 answers
Alright, the other answer was a little more complex then it needs to be.
First put the animation in a script. Then make the scripts code this:
game.Players.PlayerAdded:connect(function(player)
local anim = player.Character.Humanoid:LoadAnimation(script.AnimName)
local mouse = player:GetMouse()
mouse.KeyDown:connect(function(key)
local correctKey = "e" -- make whatever key you want
if key == correctKey then
anim:Play()
end
end)
end)
Now, if you wanted to do a key that wasn't a letter then you'd need to find the key's number and then use this code:
if string.byte(key) == correctKey then
Opposed to:
if key == correctKey then
2 comments
I figured it out on my own. Thanks for answering though.
Anytime. If you have any future questions, please feel free to message me on roblox.
Try something like this?
local player = game.Players.LocalPlayer
local humanoid = player.Character:WaitForChild("Humanoid") -- Wait for the Humanoid to exist
local animation = Instance.new("Animation")
animation.AnimationId = "http://www.roblox.com/Asset?ID=ASSETID" -- Replace ASSETID with the id of your animation
local animTrack = Humanoid:LoadAnimation(animation)
game:GetService("UserInputService").InputBegan:connect(function(inputObject)
local keycode = 0 -- Replace 0 with the keycode of your key
if inputObject.KeyCode == keycode then
animTrack:Play()
end
end)
Find a list of keycodes on the wiki: http://wiki.roblox.com/index.php?title=API:Enum/KeyCode
4 comments
If im correct. Isnt 0 For left shift?
It says Humanoid is not a valid member of script.
Where do i put the script? Startpack ? Etc
0 isn't = to shift. http://wiki.roblox.com/index.php?title=Keyboard_input_%28deprecated%29 visit that to find a lot of the codes.
You may have figured it out but this may be the best answer.
This is what I used:
- UserInputService
- Local Script
local uis = game:GetService("UserInputService")
local plyr = game:GetService("Players").LocalPlayer
local anim = Instance.new("Animation")
anim.AnimationId = "rbxasset://" --Animation id here
uis.InputBegan:connect(function(key, thing)
local char = plyr:WaitForChild("Character")
if key.KeyCode == Enum.KeyCode.A and thing then --Could be any letter(capitalized), Space, Escape, etc.
local h = char:WaitForChild("Humanoid")
h:LoadAnimation(anim):Play() --Custom animations run for 2 seconds(as of 2015)
end
end)
Hope it helps!
Enum works a little different from the string.byte(). If you go here you can see in the first line, there is the key, the second there is the Value and the end is the description(which isn't added yet). So from this chart, 0 is actually the "Unknown" key.
This discussion is preserved as it was. New answers, comments, and votes are closed.

3 comments
Help appreciated
Halp
Sorry I had to answer so late!