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

How do I play a animation when I press a a certain key?

Asked by 7 years ago
Edited 7 years ago

I tried to make a animation play by pressing Q. Nothing happened. Can you guys help me out?

local UserInputService = game:GetService("UserInputService") local localPlayer = game.Players.LocalPlayer

local animation = Instance.new("Animation") animation.AnimationId = "http://www.roblox.com/Asset?ID=492283384"

local animTrack = nil
local canPlay = true

local function onInputBegan(input, gameProcessed)
    if input.UserInputType == Enum.UserInputType.Keyboard then
        local keyPressed = input.KeyCode
        if keyPressed == Enum.KeyCode.Q then
    local player = game.Players.LocalPlayer.Character
        canPlay = false
        animTrack = player.Humanoid:LoadAnimation(animation) -- Load animation into Humanoid
        animTrack.KeyframeReached:connect(function(keyframeName) -- Bind function to KeyframeReached event
            if keyframeName == "ElectrocuteEnd" then
                canPlay = true
end

end

    end
end

local function onInputEnded(input, gameProcessed)
    if input.UserInputType == Enum.UserInputType.Keyboard then
        local keyPressed = input.KeyCode
        if keyPressed == Enum.KeyCode.Q then
     local player = game.Players.LocalPlayer.Character
        canPlay = false
        animTrack = player.Humanoid:LoadAnimation(animation) -- Load animation into Humanoid
        animTrack.KeyframeReached:connect(function(keyframeName) -- Bind function to KeyframeReached event
            if keyframeName == "ElectrocuteEnd" then
                canPlay = false
        end
    end
end

UserInputService.InputBegan:connect(onInputBegan)
UserInputService.InputEnded:connect(onInputEnded)

0
That's a free model script though... Seraine 103 — 7y
0
No. It's not. Milestmnt 5 — 7y

1 answer

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

You can use this instead its not mine so i wont use it for my game ;p


Animation = Instance.new("Animation") Animation.Parent = game.Workspace Animation.AnimationId = "http://www.roblox.com/asset/?id=ID HERE" --Where It Says ID HERE, Place Your Animation Id local Player = game.Players.LocalPlayer local mouse = game.Players.LocalPlayer:GetMouse() mouse.KeyDown:connect(function (key) for i, v in pairs(game.Players:GetChildren()) do if key == "f" then -- THIS IS UR KEY U WANA CHANGE IT CHANGE F TO SOMETHING ELSE local AnimationTrack = Player.Character.Humanoid:LoadAnimation(Animation) AnimationTrack:Play() end end end)

That is all ;)

Dont forget to accept my answer

Ad

Answer this question