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

Is there a way to link an animation made in Animation Editor to a certain key?

Asked by 8 years ago

I'm trying to make a reload animation. I have the animation of the guy and the gun. (I used DaMrNelson Character Creator to make the join for the moving parts of the gun) Then I made an animation using Animation Editor. Im now stuck on how use then animation on both player and gun when you press the "R" key

Any Tips?

0
please accept my answer Async_io 908 — 8y
1
Have Done So. Thanks! tonyv537 95 — 8y

1 answer

Log in to vote
0
Answered by
Async_io 908 Moderation Voter
8 years ago

USER INPUT SERVICE

UIS, aka User Input Service is the best way to go. UIS is used in local scripts and can be triggered by InputBegan, InputEnded, and InputChanged.

InputBegan does as you would think. It will trigger when a button is pushed.

InputEnded will trigger when a button is released.

InputChanged will trigger when a button has been changed.

An example script would be

01local uis = game:GetService('UserInputService') --Get's the service.
02 
03uis.InputBegan:connect(function(key) --the defined variable, key, is the button pushed
04    if key.KeyCode == Enum.KeyCode.Q then -- If the Key is Q
05        print("Q has been pushed!")
06    end
07end)
08uis.InputEnded:connect(function(key)
09    if key.KeyCode == Enum.KeyCode.Q then
10        print("Q has been released!")
11    end
12end)

ANIMATION

For animations, you must first define the humanoid, as you need to load the animation into the humanoid before you can play it. After you have defined the humanoid, you need to load the animation into it, which is commonly done when you're defining the animation. Then afterwards you need to play the animation.

1local player = game.Players.LocalPlayer --Defines the local player
2player.CharacterAdded:connect(function(character) --Waits for character to load, defines character
3    local humanoid = character:WaitForChild('Humanoid') --Waits for humanoid to load
4    local animation = humanoid:LoadAnimation(script.Parent.Animation) --Loads the animation into the humanoid
5    animation:Play() --Plays the animation.
6end)

COMBINED KNOWLEDGE

To roughly combine these, you would have the following script.

01local player = game.Players.LocalPlayer
02local uis = game:GetService("UserInputService")
03player.CharacterAdded:connect(function(character)
04    uis.InputBegan:connect(function(key)
05        if key.KeyCode == Enum.KeyCode.R then
06            local humanoid = character:WaitForChild('Humanoid')
07            local animation = humanoid:LoadAnimation(script.Parent.Animation)
08            animation:Play()
09        end
10    end)
11end)

Keep in mind that this is not a complete script, and you would have to add this to your gun script, and make it more complex with the ammunition and the reload animation and all that.

1
Okay. Thank You So Much! tonyv537 95 — 8y
Ad

Answer this question