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

Triggering Animation By Pressing A Key Doesnt Work?

Asked by 6 years ago
local player = game.Players.LocalPlayer
repeat wait() until player.Character.Humanoid
local humanoid = player.Character.Humanoid
local mouse = player:GetMouse()

local anim = Instance.new("Animation")
anim.AnimationId = "http://www.roblox.com/asset/?id=" -- id of animation

mouse.KeyDown:connect(function(key)
    if key == "l" then 
        local playAnim = humanoid:LoadAnimation(anim)
        playAnim:Play()
    end
end)

3 answers

Log in to vote
0
Answered by 6 years ago

Problem

Why your script isn't working is because KeyDown is deprecated and has been removed by roblox, Instead use UserInputService or ContextActionService to replace KeyDown. UserInputService has the InputBegan event which fires when the player interacts with a device, such as pressing down a key, etc. Then you could use the "input" parameter to check the key that was pressed using the KeyCode Enum.

Better Methods and Deprecated things

:connect() is deprecated, so instead use :Connect(). Use the CharacterAdded event instead of a repeat loop, and use :WaitForChild() to get the humanoid.

local player = game.Players.LocalPlayer
player.CharacterAdded:Wait()

local humanoid = player.Character:WaitForChild("Humanoid")
local mouse = player:GetMouse()
local anim = Instance.new("Animation")
local UIS = game:GetService("UserInputService")

anim.AnimationId = "http://www.roblox.com/asset/?id=" -- put the id here
anim.Parent = player.Character

UIS.InputBegan:Connect(function(input)
    if input.KeyCode = Enum.KeyCode.I then 
        local playAnim = humanoid:LoadAnimation(anim)

        playAnim:Play()
    end
end)
0
connect still works natonator63 29 — 6y
0
its a bad practice, and "if it works"; roblox can remove it at anytime User#23365 30 — 6y
0
how about the fact that KeyDown is not compatible with a computer mouse DeceptiveCaster 3761 — 6y
View all comments (2 more)
0
why the hell are people doing that, idk xd DeceptiveCaster 3761 — 6y
0
thank you GlxkTerrence 5 — 6y
Ad
Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

Client vs Server Sided

Local scripts only affect things the user sees (like GUIs, camera or local parts). Server sided scripts can change things in the server, like the animation a player does or the CFrame of a part, for example. Local scripts can't change the server or affect other users, and server sided scripts are only used for... the server. You can use Remote functions/events to let the client and server communicate with each other.


Remote Events

We're using remote events since we just want the animation to play. We will need 1 local script to record the input and 1 server sided script to play the animation.

--Remote Event is placed inside the server script in ServerScriptService
script.RemoteEvent.OnServerEvent(function(player) --When the client fires the server...
    local humanoid = player.Character:FindFirstChild("Humanoid")
    if humanoid then --If humanoid exists
        local anim = Instance.new("Animation")
        anim.AnimationId = "" --Fill this in
        local playAnim = humanoid:LoadAnimation(anim)
        playAnim:Play()
    end
end)

Local Script:

game:GetService("ServerScriptService").Script.RemoteEvent:FireServer()

UserInputService

UserInputService is much better than KeyDown (since UserInputService can get inputs from game controllers and touchscreen).

--Must be done in a local script
game:GetService("UserInputService").InputBegan:Connect(function(key)
    if key.KeyCode== Enum.KeyCode.One then
        game:GetService("ServerScriptService").Script.RemoteEvent:FireServer()
    end
end)

Use this site if you want to see the Enum index for keycodes.



Hope it helps!


Important Edit!

Apparently animations can be triggered from a local script, so remote events aren't needed. Modified code:

game:GetService("UserInputService").InputBegan:Connect(function(key)
local player = game:GetService("Players").LocalPlayer
    if key.KeyCode == Enum.KeyCode.One then
        local humanoid = player.Character:FindFirstChild("Humanoid")
        if humanoid then --If humanoid exists
            local anim = Instance.new("Animation")
            anim.AnimationId = "" --Fill this in
            local playAnim = humanoid:LoadAnimation(anim)
            playAnim:Play()
        end
    end
end)

The reason why the animation may not be running is because Animations have different priorities dictating which animation be played over another. Here is a list of enums for AnimationPriority. If you want an animation to be played, you should set its priority to something higher than the default animations (action is the highest priority). If an animation is played and another animation is played with the same priority, the first animation is stopped over the second one.


Final Product

local anim = Instance.new("Animation")
anim.AnimationId = "" --Fill this in

game:GetService("UserInputService").InputBegan:Connect(function(key)
    local player = game:GetService("Players").LocalPlayer
    if key.UserInputType == Enum.UserInputType.Keyboard then --Make sure it is a keyboard input
        if key.KeyCode == Enum.KeyCode.One then
            local humanoid = player.Character:FindFirstChild("Humanoid")
            if humanoid then --If humanoid exists
                local playAnim = humanoid:LoadAnimation(anim)
                playAnim.Priority = Enum.AnimationPriority.Action --Highest priority animation
                playAnim:Play()
            end
        end
    end
end)

Change Enum...Action to Enum...Core if you want animations like walking, falling, jumping or swimming to override it.

Log in to vote
-3
Answered by 6 years ago
Edited 6 years ago

a much better method would be doing:

local player = game.Players.LocalPlayer
repeat wait() until player.Character.Humanoid
local humanoid = player.Character.Humanoid
local mouse = player:GetMouse()
Animation = script:WaitForChild("Animation")
mouse.KeyDown:Connect(function(key)
    if key == "l" then  --REMEMBER TO INSERT A ANIMATION INTO THE SCRIPT
        local Animationtrack = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(Animation)
        Animationtrack:Play()
    end
end)
0
KeyDown is removed and doesnt work anymore User#23365 30 — 6y
0
why are you defining things multiple times when theres a variable for it User#23365 30 — 6y

Answer this question