I'm trying to make a script for when I hold the X key, the animation starts, and when I release, it stops. Well, everything was going well, until I tested it. I get the error, "Unable to cast value to Object" This happens on line 5 of my code. The code is below:
local player = game.Players.LocalPlayer
local human = player.Character:WaitForChild("Humanoid")
local animation = Instance.new("Animation")
local ca = "rbxassetid://2401982156"
local canimation = human:LoadAnimation(ca)
game:GetService("UserInputService").InputBegan:Connect(function(input,gameprocesed)
if input.KeyCode == Enum.KeyCode.X then
canimation:Play()
end
end)
game:GetService("UserInputService").InputEnded:Connect(function(input,gameprocesed)
if input.KeyCode == Enum.KeyCode.X and canimation.IsPlaying then
canimation:Stop()
end
end)
Can someone please help me?

2 comments
One little note: you can create a variable for UserInputService.
The "ca" has to be an animation instance