I want a script that uses crouching animation, and when pressing a key it enables (but animation speed is 0) when character is moving animation speed is 1, and when character stops moving animation speed is 0 again. If they press the hotkey again, they are back to walking.
I have a script but there’s a problem where it only detects when I press the hotkey, not while I am walking.
Video showing problem & what I want:
The script I have currently:
local Hotkey = "C"
local PlayerSerivce = game:GetService("Players")
local Player = PlayerSerivce.LocalPlayer
local Character = Player.Character
local Humanoid = Character:WaitForChild("Humanoid")
local AnimationID = game:GetService("ReplicatedStorage").Crawl.AnimationId
local CharacterAnimation
game:GetService("UserInputService").InputBegan:Connect(function(inputObject, gameProcessedEvent)
if inputObject.KeyCode == Enum.KeyCode[Hotkey] then
animation()
end
end)
function animation()
if Character then
local CrawlAnimation = Character:FindFirstChild("AnimationCharacter")
if CharacterAnimation then
CharacterAnimation:Stop()
end
if CrawlAnimation then
if CrawlAnimation.AnimationId == AnimationID then
CrawlAnimation:Destroy()
return
end
CrawlAnimation:Destroy()
end
local Animation =Instance.new("Animation",Character)
Animation.Name = "AnimationCharacter"
Animation.AnimationId = AnimationID
CharacterAnimation = Character.Humanoid:LoadAnimation(Animation)
if Humanoid.MoveDirection.Magnitude > 0 then
CharacterAnimation:Play()
CharacterAnimation:AdjustSpeed(1)
end
if Humanoid.MoveDirection.Magnitude <= 0 then
CharacterAnimation:Play()
CharacterAnimation:AdjustSpeed(0)
end
end
end