MoveDirection Question

Hello,

the character executes the animation for the sprint only if I move to the right or the left to update the value but the animation does not happen when I go in front of me.

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 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
	end
end)

UIS.InputEnded:Connect(function(keycode, _gameProcess)
	if keycode.KeyCode == Enum.KeyCode.LeftShift and not _gameProcess then
		--rs.Events.Movement.SetSprint:FireServer(false)
		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
	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)
1 Like