Sneaking script not working the way it is supposed to be

I want to achieve a working sneaking script where if you do not run the animation will not play and if it is playing and you stop running, the animation will stop aswelll, the issue is that it sometimes just doesn’t play at all or it stops in middle of running or it starts playing while you stand still.

Localscript:

local UIS = game:GetService("UserInputService")
local Character = script.Parent
local Humanoid = Character:FindFirstChild("Humanoid")
local Sneak_Walk_Speed = 8
local Normal_Walk_Speed = 16

local Animations = {
	Sneak = Humanoid:LoadAnimation(game.ReplicatedStorage.Animations:WaitForChild("Sneak"))
}
local AnimationBooleans = {
	Sneak = false
}

UIS.InputBegan:Connect(function(InputKey,IsTyping)
	if not IsTyping then
		if InputKey.KeyCode == Enum.KeyCode.LeftShift then
			Humanoid.WalkSpeed = Sneak_Walk_Speed
			AnimationBooleans.Sneak = not AnimationBooleans.Sneak
		end
	end
end)
UIS.InputEnded:Connect(function(InputKey,IsTyping)
	if not IsTyping then
		if InputKey.KeyCode == Enum.KeyCode.LeftShift then
			Humanoid.WalkSpeed = Normal_Walk_Speed
			AnimationBooleans.Sneak = not AnimationBooleans.Sneak
		end
	end
end)

local IsRunning = false

Humanoid.Running:Connect(function(Speed)
	if Speed < Sneak_Walk_Speed then
		IsRunning = not IsRunning
	else
		IsRunning = not IsRunning
		if Animations.Sneak.IsPlaying then
			Animations.Sneak:Stop()
		end
	end
end)

while wait() do
	if AnimationBooleans.Sneak and IsRunning then
		Animations.Sneak:Play()
		Animations.Sneak.Stopped:Wait()
	end
end

Make sure you published the animation as a movement animation

The animation was already published as Movement