Run when walking forward

I want to make the player do a running animation when walking forward and increase the walkspeed, and when they stop walking forward the walkspeed returns to normal and the animation stops.

I’ve tried many different ways, but here’s the current code. If anyone knows an efficient way or a better way to do this please share.

LOCAL SCRIPT

Humanoid.Running:Connect(function()
	local walkingforward = false
	local walkingbackwards = false
	if not stunned and not attacking and not blocking then
	local animation = Instance.new("Animation")
		animation.AnimationId = 'rbxassetid://18341337226'
	local animationtrack = Humanoid:FindFirstChildOfClass("Animator"):LoadAnimation(animation)
	local direction = Character:GetPivot():VectorToObjectSpace(Character.PrimaryPart.AssemblyLinearVelocity)
			if direction.Z < -0.1 then
				walkingforward = true
				walkingbackwards = false
			elseif direction.Z > -0.1 and direction.Z < 0 then
				walkingforward = false
				walkingbackwards = false
			elseif direction.Z < 0.1 then
				walkingforward = false
				walkingforward = true
			end
	end
end)

Here is a basic script that should do what you described.

local animation = humanoid.Animator:LoadAnimation(–[[script.Animation]]) – Make sure that the animation priority is higher than the default running animation

while task.wait(0.1) do

local running = true

if humanoid:GetState() == Enum.HumanoidStateType.Running then

  local dir = humanoid.MoveDirection:Dot(HRP.CFrame.LookVector) 
  
  if dir >= DIRECTION_SENSETIVITY then
  	
  	humanoid.WalkSpeed = 32
  	
  	animation:Play()
  	
  else
  	running = false
  end

else
running = false
end

if running == false then
humanoid.WalkSpeed = 16
animation:Stop()
end

end

One way to check if a character is moving forwards is to calculate the dot product of its Humanoid’s MoveDirection with the current camera’s LookVector:

local RunService = game:GetService("RunService")
local Workspace = game:GetService("Workspace")

local Animation = Instance.new("Animation")
Animation.AnimationId = "rbxassetid://18341337226"

local AnimationTrack = Humanoid:WaitForChild("Animator"):LoadAnimation(Animation)

local Camera = Workspace.CurrentCamera or Workspace:WaitForChild("Camera")

local PrimaryPart = Character.PrimaryPart

local x = 0 -- Not recommended to edit

local y = 4 -- How many seconds to reach max speed, or slow down back to 0

local maxSpeed = 32 - Humanoid.WalkSpeed -- 32 is the desired max speed. Needs to be subtracted by default speed

RunService.PostSimulation:Connect(function(deltaTime)
	local direction = Humanoid.MoveDirection:Dot(Camera.CFrame.LookVector)

	if direction > 0.8 then -- Moving forwards
		x = math.min(x + (deltaTime / y), 1)

		if not AnimationTrack.IsPlaying then
			AnimationTrack:Play()
		end
	else
		x = math.max(x - (deltaTime / y), 0)

		if AnimationTrack.IsPlaying and x == 0 then
			AnimationTrack:Stop()
		end
	end

	Humanoid.WalkSpeed = (maxSpeed * x) + 16
end)

I used a RunService loop so that the movement direction is checked regularly, and to use its deltaTime parameter to smoothly increase the walk speed :slight_smile::+1:

1 Like

I like this script a lot. Though the speed up and speed down isn’t what im looking for it WILL be used in future projects trust me. Thanks for the input! :smirk_cat::+1:

1 Like

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