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

Animation won't play when shift is pressed?

Asked by 4 years ago

Hi, I made a sprint script and tried to make an animation play when I press shift. Only problem is, it won't play. I tried adjusting the priority and put it in different parts of the script but it doesn't work.

local anim = Instance.new("Animation")
anim.AnimationId = "rbxassetid://2510238627"

local plr = game:GetService("Players").LocalPlayer
local mouse = plr:GetMouse()

local char = plr.Character or plr.CharacterAdded:Wait()
local human = char:WaitForChild("Humanoid")

local track = human:LoadAnimation(anim)
track.Priority = Enum.AnimationPriority.Action

mouse.KeyDown:Connect(function(k)
    if k:lower():byte() == 48 then
        track:Play()
        human.WalkSpeed = 23
    end
end)

mouse.KeyUp:Connect(function(k)
    if k:lower():byte() == 48 then
        track:Stop()
        human.WalkSpeed = 16
    end
end)

1 answer

Log in to vote
1
Answered by 4 years ago

Hello. Try using UserInputService as Mouse.KeyDown is deprecated. Try this:

local UserInputService = game:GetService("UserInputService")

local anim = Instance.new("Animation")
anim.AnimationId = "rbxassetid://2510238627"

local plr = game:GetService("Players").LocalPlayer
local mouse = plr:GetMouse()

local char = plr.Character or plr.CharacterAdded:Wait()
local human = char:WaitForChild("Humanoid")

local track = human:LoadAnimation(anim)
track.Priority = Enum.AnimationPriority.Action

UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
    if input.KeyCode == Enum.KeyCode.LeftShift and not gameProcessedEvent then
        track:Play()
        human.WalkSpeed = 23
    end
end)

UserInputService.InputEnded:Connect(function(input, gameProcessedEvent)
    if input.KeyCode == Enum.KeyCode.LeftShift and not gameProcessedEvent then
           track:Stop()
        human.WalkSpeed = 16
    end
end)

The InputBegan function fires when an input is detected. Then there's an if statement checking if the input is left shift and that the player isn't in chat. Then it plays the animation and changes the walkspeed.

0
It works, but my animation doesn't change. It's supposed to change my walking animation to the animation in the script. mybituploads 304 — 4y
0
If it matters I'm using the werewolf run animation mybituploads 304 — 4y
0
Try setting the animation's priority inside the animation editor. youtubemasterWOW 2741 — 4y
0
Nevermind, the animation ID was incorrect. mybituploads 304 — 4y
0
oof youtubemasterWOW 2741 — 4y
Ad

Answer this question