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

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

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?

2 comments

0

please accept my answer

1

Have Done So. Thanks!

1 answer

✓ Accepted answer

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

local uis = game:GetService('UserInputService') --Get's the service.

uis.InputBegan:connect(function(key) --the defined variable, key, is the button pushed
	if key.KeyCode == Enum.KeyCode.Q then -- If the Key is Q
		print("Q has been pushed!")
	end
end)
uis.InputEnded:connect(function(key)
	if key.KeyCode == Enum.KeyCode.Q then
		print("Q has been released!")
	end
end)

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.

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

COMBINED KNOWLEDGE

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

local player = game.Players.LocalPlayer
local uis = game:GetService("UserInputService")
player.CharacterAdded:connect(function(character)
	uis.InputBegan:connect(function(key)
		if key.KeyCode == Enum.KeyCode.R then
			local humanoid = character:WaitForChild('Humanoid') 
			local animation = humanoid:LoadAnimation(script.Parent.Animation) 
			animation:Play()
		end
	end)
end)

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 comment

1

Okay. Thank You So Much!

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