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

How can i play an animation when I press a key?

Lets say i have an animation called "Punch" and it consists of a punching animation and I saved it. Pretend the ID was 161897876. How would i script it so it plays when i press Q? I have been learning Lua for a week now can understand some of the basic parts (such as Variables,Functions etc.).

2 comments

0
0

You can use the same site king' linked above to search more information on animations too.

1 answer

✓ Accepted answer

LocalScript somewhere under StarterGui

local player = game.Players.LocalPlayer
repeat wait() until workspace:FindFirstChild(player.Name) ~= nil

local animation = Instance.new("Animation", game.ReplicatedStorage)
animation.AnimationId = "rbxassetid://161897876"
local animPlayer = player.Character.Humanoid:LoadAnimation(animation)

local debounce = true

game:GetService("UserInputService").InputBegan:Connect(function(inputObject, gameProcessedEvent)
	if inputObject.KeyCode == Enum.KeyCode.Q then
	if debounce == false then return end
	debounce = false
		animPlayer:Play()
		wait(2)
	debounce = true
	end
end)

Ok, so to find when the player chooses the key "Q" use the UserInputService (UIS), this was talked about in the Comments Section, where there is a helpful link, if you need it.

First, we need to wait for the player's Character, then load the animation into their Humanoid

The function also has a debounce in case the player tries to choose "Q" repeatedly

The "wait(2)" is meant to be the time for the animation to play (the animation editor's default is 2 seconds)

You must also own the animation for it to be played, if this is for a Group Game, the owner of the group must have published the animation.

1 comment

0

Thank you this helped alot

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