Player Shift Button Input For Animation?

I was just scripting something for my sliding animation, but I want the script to detect and play the animation when the player presses the shift button. Right now, the script detects when the player presses the C button. How would I change this?
Here’s my code:

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local Animate 
local Humanoid = player.Character:FindFirstChild('Humanoid')

mouse.KeyDown:Connect(function(Key) 
	if Key == "c" then
		local Animation = Instance.new("Animation", player.Character)
		Animation.AnimationId = "rbxassetid://5771537537"
		Animate = Humanoid:LoadAnimation(Animation)
		Animate:Play()
	end  
end)

The error is in if Key == "c". Try writing if Key == "LeftShift" or RightShift depending on which one you want players to use.
Also, be careful of the animation, in order to work it has to be yours and only yours, or the script won’t work and return a “Failed animation to load” error. Hope this helped!

1 Like

Thanks! (By the way, the animation is mine :])

I just edited the script and tried with LeftShift. However, nothing happens. Do you know why?

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local Animate 
local Humanoid = player.Character:FindFirstChild('Humanoid')

mouse.KeyDown:Connect(function(Key) 
	if Key == "LeftShift" then
		local Animation = Instance.new("Animation", player.Character)
		Animation.AnimationId = "rbxassetid://5771537537"
		Animate = Humanoid:LoadAnimation(Animation)
		Animate:Play()
	end  
end)

All that happens is that Shiftlock is toggled. (I have it on.)

I just tried disabling ShiftLock but it still didn’t work.

I don’t code since a while, but I suggest changing the shift lock key to another one (like U or V etc).
As for the animation try using Enum or the UIS (UserInputService)

1 Like

I improved your script a little bit, try this:

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")

local UIS = game:GetService("UserInputService")

local Animation = Instance.new("Animation")
Animation.AnimationId = "rbxassetid://5771537537"
Animation.Parent = Character

local Animate

UIS.InputBegan:Connect(function(Input)
  if Input.KeyCode == Enum.KeyCode.LeftShift then
    if Animate then
      Animate:Stop()
      Animate = nil
    else
      Animate = Humanoid:LoadAnimation(Animation)
      Animate:Play()
    end
  end
end)
2 Likes