Walking animation / Running animation playing after landing

I’ve made a very basic custom animation script and it actually works so far aside from this broken part, whenever a player jumps and presses any directional movement key(WASD) the script breaks and plays the walking animation on loop, after moving it’ll stop but I can’t seem to figure out how to get it to stop

I’ve tried doing Humanoid.Jumping and the get state changed for landed so it plays the idle but nothing works
All help is appreciated, thank you!

1 Like

you should put a big if statement and a function called RefreshAnimations and check the stuff in there

also send it in a code block I’m dying

local Character = script.Parent
local Humanoid: Humanoid = Character:WaitForChild("Humanoid")

local CivIdle = Instance.new("Animation")
local CivWalk = Instance.new("Animation")
local CivRun = Instance.new("Animation")
CivIdle.AnimationId = "rbxassetid://11851474629"
CivWalk.AnimationId = "rbxassetid://1191461477"
CivRun.AnimationId = "rbxassetid://12256150958"
local CivIdleTrack: AnimationTrack = Humanoid:WaitForChild("Animator"):LoadAnimation(CivIdle)
local CivWalkTrack: AnimationTrack = Humanoid:WaitForChild("Animator"):LoadAnimation(CivWalk)
local CivRunTrack: AnimationTrack = Humanoid:WaitForChild("Animator"):LoadAnimation(CivRun)
CivIdleTrack.Priority = Enum.AnimationPriority.Idle
CivWalkTrack.Priority = Enum.AnimationPriority.Movement
CivRunTrack.Priority = Enum.AnimationPriority.Movement

local UIS = game:GetService("UserInputService")
local IsRunning = false

CivIdleTrack:Play()

local function RefreshAnimations()
	local speed = Humanoid.MoveDirection.Magnitude
	if speed > 0 then
		if IsRunning then
			if CivWalkTrack.IsPlaying then
				CivWalkTrack:Stop()
			end

			if CivIdleTrack.IsPlaying then
				CivIdleTrack:Stop()
			end

			if not CivRunTrack.IsPlaying then
				CivRunTrack:Play()
			end
		else
			if CivIdleTrack.IsPlaying then
				CivIdleTrack:Stop()
			end

			if CivRunTrack.IsPlaying then
				CivRunTrack:Stop()
			end

			if not CivIdleTrack.IsPlaying then
				CivWalkTrack:Play()
				CivWalkTrack:AdjustSpeed(1.3)
			end
		end
	elseif speed == 0 then
		if CivWalkTrack.IsPlaying then
			CivWalkTrack:Stop()
		end

		if CivRunTrack.IsPlaying then
			CivRunTrack:Stop()
		end

		if not CivIdleTrack.IsPlaying then
			CivIdleTrack:Play()
		end
	end
end

Humanoid.Running:Connect(RefreshAnimations)
Humanoid.StateChanged:Connect(function(_, state)
	if state == Enum.HumanoidStateType.FallingDown or state == Enum.HumanoidStateType.Jumping then
		CivWalkTrack:Stop()
		CivRunTrack:Stop()
		CivIdleTrack:Stop()
	elseif state == Enum.HumanoidStateType.Landed then
		RefreshAnimations()
	end
end)

UIS.InputBegan:Connect(function(input, isTyping)
	if input.KeyCode == Enum.KeyCode.LeftControl then
		if isTyping then return else 
			IsRunning = not IsRunning
			if IsRunning == true then
				Humanoid.WalkSpeed = 24
			else
				Humanoid.WalkSpeed = 13
			end
			RefreshAnimations()
		end
	end
end)
2 Likes

how do I make a block code, I apologize

and thank you so much you have no clue how much this helped

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.