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

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

Asked by 5 years ago

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.).

0
0
You can use the same site king' linked above to search more information on animations too. xPolarium 1388 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

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.

0
Thank you this helped alot jazzyfunkyman -3 — 5y
Ad

Answer this question