How Do I Fix My Movement Animation Script

I’m making a game and I’ve made a basic-ish script for running, but every time my character stops it keeps playing the walking animation. I though I could just stick a little piece of code to stop all animations and sounds when walkspeed = 0 but now the player can only move if they run (hold in shift).

What do I do to make this work?

Here’s the script:

local player = game.Players.LocalPlayer
local character = script.Parent
local humanoid = character:WaitForChild("Humanoid")

local walkAnim = script:WaitForChild("WalkAnimation")
local walkAnimTrack = humanoid.Animator:LoadAnimation(walkAnim)

local walkSound = script:WaitForChild("Walking Sound")

local runAnim = script:WaitForChild("RunAnimation")
local runAnimTrack = humanoid.Animator:LoadAnimation(runAnim)

local runSound = script:WaitForChild("Running Sound")


game:GetService("UserInputService").InputBegan:Connect(function(input, process)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		if player.Character:FindFirstChild("Humanoid") then
			player.Character:FindFirstChild("Humanoid").WalkSpeed = 32
			print("Running")
			walkAnimTrack:Stop()
			runAnimTrack:Play()
			walkSound:Stop()
			runSound:Play()
			
		end
	end
end)
game:GetService("UserInputService").InputEnded:Connect(function(input, process)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		if player.Character:FindFirstChild("Humanoid") then
			player.Character:FindFirstChild("Humanoid").WalkSpeed = 18
			print("Walking")
			runAnimTrack:Stop()
			runSound:Stop()
			walkAnimTrack:Play()
			walkSound:Play()
		end
	end
end)

This is the little piece of code I’ve tried inserting under the walking part:

if player.Character:FindFirstChild("Humanoid") then
	player.Character:FindFirstChild("Humanoid").WalkSpeed = 0
	print("Idle")
	runAnimTrack:Stop()
	runSound:Stop()
	walkAnimTrack:Stop()
	walkSound:Stop()
end	

Anything helps!

  • Logo

You set walkspeed to zero instead of test it on a if statement.

You should be able to detect walk speed with a Humanoid.Running function

player.Character:FindFirstChild("Humanoid").Running:Connect(function(speed)
--code
end)