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

Help making a script that starts an animation when you press a key?

Asked by 9 years ago

Im relatively new to scripting, So I need some help with this. I have the run animation ready.

0
Help appreciated Lazynathaniel 10 — 9y
0
Halp Lazynathaniel 10 — 9y
0
Sorry I had to answer so late! EzraNehemiah_TF2 3552 — 9y

3 answers

Log in to vote
0
Answered by 9 years ago

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 
0
I figured it out on my own. Thanks for answering though. Lazynathaniel 10 — 9y
0
Anytime. If you have any future questions, please feel free to message me on roblox. ObscureEntity 294 — 9y
Ad
Log in to vote
1
Answered by 9 years ago

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

0
If im correct. Isnt 0 For left shift? Lazynathaniel 10 — 9y
0
It says Humanoid is not a valid member of script. Lazynathaniel 10 — 9y
0
Where do i put the script? Startpack ? Etc Lazynathaniel 10 — 9y
0
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. ObscureEntity 294 — 9y
Log in to vote
0
Answered by 9 years ago

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.

Answer this question