Animate Script does not stop animations

Hello, why my sprint/walk animations is not stopped when I do not move sometimes ?
image

https://gyazo.com/d21a4630bf1a4355aa45b9d9bd76ae9f

Localscript :

local rf = game:GetService("ReplicatedFirst")
local rs = game:GetService("ReplicatedStorage")
local UIS = game:GetService("UserInputService")

local plr = game:GetService("Players").LocalPlayer
local character = script.Parent

local humanoid = character:FindFirstChild("Humanoid")

repeat task.wait() until humanoid

local walkAnim = script:WaitForChild("Walk")
local pauseAnim = script:WaitForChild("Pause")
local sprintAnim = script:WaitForChild("Sprint")
local walkAnimTrack = humanoid.Animator:LoadAnimation(walkAnim)
local pauseAnimTrack = humanoid.Animator:LoadAnimation(pauseAnim)
local sprintAnimTrack = humanoid.Animator:LoadAnimation(sprintAnim)

UIS.InputBegan:Connect(function(keycode, _gameProcess)
	if humanoid.MoveDirection ~= Vector3.new(0, 0, 0)then
		if keycode.KeyCode == Enum.KeyCode.LeftShift and not _gameProcess then
			if humanoid:FindFirstChild("Sprint") then  humanoid:FindFirstChild("Sprint").Value = true
			else 
				local v = Instance.new("BoolValue", character:FindFirstChildOfClass("Humanoid"))
				v.Name = "Sprint"
				v.Value = true
			end

			walkAnimTrack:Stop()
			sprintAnimTrack:Play()
			humanoid.WalkSpeed = 25
		end
	end
end)
UIS.InputEnded:Connect(function(keycode, _gameProcess)
	if humanoid.MoveDirection ~= Vector3.new(0, 0, 0) then
		if keycode.KeyCode == Enum.KeyCode.LeftShift and not _gameProcess then
			if humanoid:FindFirstChild("Sprint") then humanoid:FindFirstChild("Sprint").Value = false
			else 
				local v = Instance.new("BoolValue", character:FindFirstChildOfClass("Humanoid"))
				v.Name = "Sprint"
				v.Value = false
			end

			sprintAnimTrack:Stop()
			walkAnimTrack:Play()
			humanoid.WalkSpeed = 16
		end
	end
end)

humanoid:GetPropertyChangedSignal("MoveDirection"):Connect(function()
	if humanoid.MoveDirection == Vector3.new(0, 0, 0) and walkAnimTrack.IsPlaying then
		if walkAnimTrack.IsPlaying then
			walkAnimTrack:Stop()
		end

		if sprintAnimTrack.IsPlaying then
			sprintAnimTrack:Stop()
		end

		if not pauseAnimTrack.IsPlaying then
			pauseAnimTrack:Play()
		end
	else 
		--local getSprint = rs.Events.Movement.GetSprint:InvokeServer()
		local getSprint = humanoid:FindFirstChild("Sprint")
		if pauseAnimTrack.IsPlaying then
			pauseAnimTrack:Stop()
		end

		if getSprint and getSprint.Value then 
			if walkAnimTrack.IsPlaying then
				walkAnimTrack:Stop()
			end

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

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

Are you trying to do custom player animation or something?

Just use .Running connection to detect if player is walking or not.

Yes, I tried a lot of things and it’s the best thing I done but there is still a problem.

Yeah and how I know if he is not “Running” is only when the player is moving, no in these 2 cases (moving or not).

Inside running, if not don’t use Once.

Humanoid.Changed:Once(function(Property)
   if Property == "MoveDirection" then
      if Humanoid.MoveDirection.Magnitude <= 0 then
         print("stopped LOLOLO")
      end
   end
end)

Alright I will try, I will send you the new code soon.

local rf = game:GetService("ReplicatedFirst")
local rs = game:GetService("ReplicatedStorage")
local UIS = game:GetService("UserInputService")

local plr = game:GetService("Players").LocalPlayer
local character = script.Parent

local humanoid = character:FindFirstChild("Humanoid")

local walkAnim = script:WaitForChild("Walk")
local pauseAnim = script:WaitForChild("Pause")
local sprintAnim = script:WaitForChild("Sprint")

local walkAnimTrack = humanoid.Animator:LoadAnimation(walkAnim)
local pauseAnimTrack = humanoid.Animator:LoadAnimation(pauseAnim)
local sprintAnimTrack = humanoid.Animator:LoadAnimation(sprintAnim)

local isSprinting = script.IsSprinting

UIS.InputBegan:Connect(function(key, _q)
	if key.KeyCode == Enum.KeyCode.LeftShift and not _q then 
		humanoid.WalkSpeed = 25
		isSprinting.Value = true
	end
end)

UIS.InputEnded:Connect(function(key, _q)
	if key.KeyCode == Enum.KeyCode.LeftShift and not _q then 
		humanoid.WalkSpeed = 16
		isSprinting.Value = false
	end
end)

humanoid.Changed:Connect(function(Property)
	if Property == "MoveDirection" then
		if humanoid.MoveDirection.Magnitude <= 0 then
			if not pauseAnimTrack.IsPlaying then
				pauseAnimTrack:Play()
			end
			walkAnimTrack:Stop()
			sprintAnimTrack:Stop()
		elseif humanoid.MoveDirection.Magnitude > 0 then 
			if isSprinting.Value then 
				if not sprintAnimTrack.IsPlaying then 
					sprintAnimTrack:Play()
				end
				walkAnimTrack:Stop()
				pauseAnimTrack:Stop()
			else 
				if not walkAnimTrack.IsPlaying then 
					sprintAnimTrack:Play()
				end
				pauseAnimTrack:Stop()
				sprintAnimTrack:Stop()
			end
		end
	end
end)

There is a little problem : https://gyazo.com/787bfe479829f6854c482019913a2a11

I tried to add if but there is still this “glitch”, like if I was blocked :

local rf = game:GetService("ReplicatedFirst")
local rs = game:GetService("ReplicatedStorage")
local UIS = game:GetService("UserInputService")

local plr = game:GetService("Players").LocalPlayer
local character = script.Parent

local humanoid = character:FindFirstChild("Humanoid")

local walkAnim = script:WaitForChild("Walk")
local pauseAnim = script:WaitForChild("Pause")
local sprintAnim = script:WaitForChild("Sprint")

local walkAnimTrack = humanoid.Animator:LoadAnimation(walkAnim)
local pauseAnimTrack = humanoid.Animator:LoadAnimation(pauseAnim)
local sprintAnimTrack = humanoid.Animator:LoadAnimation(sprintAnim)

local isSprinting = script.IsSprinting

UIS.InputBegan:Connect(function(key, _q)
	if key.KeyCode == Enum.KeyCode.LeftShift and not _q then 
		humanoid.WalkSpeed = 25
		isSprinting.Value = true
	end
end)

UIS.InputEnded:Connect(function(key, _q)
	if key.KeyCode == Enum.KeyCode.LeftShift and not _q then 
		humanoid.WalkSpeed = 16
		isSprinting.Value = false
	end
end)

humanoid:GetPropertyChangedSignal("MoveDirection"):Connect(function()
	if humanoid.MoveDirection == Vector3.new(0, 0, 0) then
		if not pauseAnimTrack.IsPlaying then
			pauseAnimTrack:Play()
		end
		
		if walkAnimTrack.IsPlaying then
			walkAnimTrack:Stop()
		end
		if sprintAnimTrack.IsPlaying then
			sprintAnimTrack:Stop()
		end
	elseif humanoid.MoveDirection.Magnitude > 0 then 
		if isSprinting.Value then 
			if not sprintAnimTrack.IsPlaying then 
				sprintAnimTrack:Play()
			end
			
			if walkAnimTrack.IsPlaying then
				walkAnimTrack:Stop()
			end
			if pauseAnimTrack.IsPlaying then
				pauseAnimTrack:Stop()
			end
		else 
			if not walkAnimTrack.IsPlaying then 
				sprintAnimTrack:Play()
			end
			
			if pauseAnimTrack.IsPlaying then
				pauseAnimTrack:Stop()
			end
			if sprintAnimTrack.IsPlaying then
				sprintAnimTrack:Stop()
			end
		end
	end
end)
1 Like

By the way nothing is anchored…


Well, I just removed walk.isPlaying and it works :

local rf = game:GetService("ReplicatedFirst")
local rs = game:GetService("ReplicatedStorage")
local UIS = game:GetService("UserInputService")

local plr = game:GetService("Players").LocalPlayer
local character = script.Parent

local humanoid = character:FindFirstChild("Humanoid")

local walkAnim = script:WaitForChild("Walk")
local pauseAnim = script:WaitForChild("Pause")
local sprintAnim = script:WaitForChild("Sprint")
local walkAnimTrack = humanoid.Animator:LoadAnimation(walkAnim)
local pauseAnimTrack = humanoid.Animator:LoadAnimation(pauseAnim)
local sprintAnimTrack = humanoid.Animator:LoadAnimation(sprintAnim)

UIS.InputBegan:Connect(function(keycode, _gameProcess)
	if humanoid.MoveDirection ~= Vector3.new(0, 0, 0)then
		if keycode.KeyCode == Enum.KeyCode.LeftShift and not _gameProcess then
			if humanoid:FindFirstChild("Sprint") then  humanoid:FindFirstChild("Sprint").Value = true
			else 
				local v = Instance.new("BoolValue", character:FindFirstChildOfClass("Humanoid"))
				v.Name = "Sprint"
				v.Value = true
			end

			walkAnimTrack:Stop()
			sprintAnimTrack:Play()
			humanoid.WalkSpeed = 25
		end
	end
end)
UIS.InputEnded:Connect(function(keycode, _gameProcess)
	if humanoid.MoveDirection ~= Vector3.new(0, 0, 0) then
		if keycode.KeyCode == Enum.KeyCode.LeftShift and not _gameProcess then
			humanoid:FindFirstChild("Sprint").Value = false
			sprintAnimTrack:Stop()
			walkAnimTrack:Play()
			humanoid.WalkSpeed = 16
		end
	end
end)

humanoid:GetPropertyChangedSignal("MoveDirection"):Connect(function()
	if humanoid.MoveDirection == Vector3.new(0, 0, 0) then
		if walkAnimTrack.IsPlaying then
			walkAnimTrack:Stop()
		end

		if sprintAnimTrack.IsPlaying then
			sprintAnimTrack:Stop()
		end

		if not pauseAnimTrack.IsPlaying then
			pauseAnimTrack:Play()
		end
	else 
		--local getSprint = rs.Events.Movement.GetSprint:InvokeServer()
		local getSprint = humanoid:FindFirstChild("Sprint")
		if pauseAnimTrack.IsPlaying then
			pauseAnimTrack:Stop()
		end

		if getSprint and getSprint.Value then 
			if walkAnimTrack.IsPlaying then
				walkAnimTrack:Stop()
			end

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

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

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