Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to pause and unpause animation based on Character Movement?

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: https://drive.google.com/file/d/1q_DGXxo3Hxlfujl1KzhPNL-SYIrgYnJU/view

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


1 answer

i fixed the script for you, you need to detect if the player is moving using Humanoid.Running or walking so you can stop the animation when plr is not moving

local Hotkey = "C"
local PlayerSerivce = game:GetService("Players")
local Player = PlayerSerivce.LocalPlayer
local Character = Player.Character
local Humanoid = Character:WaitForChild("Humanoid")
local CharacterAnimation
local IsCrawl = false
CharacterAnimation = Character.Humanoid:LoadAnimation(script:WaitForChild("Crawl"))
local function Animate()
	Humanoid.WalkSpeed = 5
	if not IsCrawl then
		CharacterAnimation:Play()
		CharacterAnimation:AdjustSpeed(0)
		Humanoid:SetStateEnabled(Enum.HumanoidStateType.Climbing, false)
		Humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping, false)
		IsCrawl = true
		Humanoid.Running:Connect(function(speed)
			if speed > 0 then
				CharacterAnimation:AdjustSpeed(speed/5)
			else
				CharacterAnimation:AdjustSpeed(0)
			end
		end)
	elseif IsCrawl then
		IsCrawl = false
		Humanoid.WalkSpeed = 16
		CharacterAnimation:Stop()
		Humanoid:SetStateEnabled(Enum.HumanoidStateType.Climbing, true)
		Humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping, true)
	end
end

game:GetService("UserInputService").InputBegan:Connect(function(inputObject, gameProcessedEvent)
	if inputObject.KeyCode == Enum.KeyCode[Hotkey] then
		Animate()
	end
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.