[unsolved] Need help with directional walking script

I’ve found this really good script on some other devforum post, so thanks to whoever put the time making this! But, theres a problem, for some reason the animations do not play. I’ve done some tweaking and testing to see if the script is functional, and all seems well, but the animations just don’t play.

Basically, the script detects whenever the player walks in a certain direction. For example, I move my character forward, the script detects this change and so changes the main walking animation (run anim in animate script) to the forward walking animation id. I can prove this works by printing which direction the character walks to and the change in animation ID

default run animation id is 11413856593
Ex 1)

This script is open source and can be found here

local Character = script.Parent.Parent
local Humanoid = Character:WaitForChild("Humanoid")
local HumanoidRoot = Character:WaitForChild("HumanoidRootPart")
-----AnimationHandler
local Animate = script.Parent
local RelativeWalk = script.RelativeWalk
---Animations' Names
local WalkF = RelativeWalk.WalkAnimFront
local WalkB = RelativeWalk.WalkAnimBack
local WalkR = RelativeWalk.WalkAnimRight
local WalkL = RelativeWalk.WalkAnimLeft
----MainWalk^^^
local MainWalk = script.Parent.run.RunAnim
-----Varibles
local RunSer = game:GetService("RunService")
--(*)
local CurrentAnim = nil 

-----Functions^^}

function ToWalkRelativeToHRP(Humanoid, Direction, RootCompare)

	-----Check if player is moving
	if Direction ~= nil then
		----X-x-x Continue

		local RootFacing = RootCompare.CFrame.LookVector
		local RootRight = RootCompare.CFrame.RightVector

		local DirectionRelativeToRootFront = RootFacing:Dot(Direction, RootFacing)
		local DirectionRelativeToRootSides = RootRight:Dot(Direction, RootRight)
		----

		local Output = nil

		if DirectionRelativeToRootFront <= 1 and DirectionRelativeToRootFront >= 0.75 then
			Output = "Forwards"
			MainWalk.AnimationId = WalkF.AnimationId
		elseif DirectionRelativeToRootFront >= -1 and DirectionRelativeToRootFront <= -0.75 then
			Output = "Backwards"
			MainWalk.AnimationId = WalkB.AnimationId
		elseif DirectionRelativeToRootSides <= 1 and DirectionRelativeToRootSides >= 0.75 then
			Output = "Right"
			MainWalk.AnimationId = WalkR.AnimationId
		elseif DirectionRelativeToRootSides >= -1 and DirectionRelativeToRootSides <= -0.75 then
			Output = "Left"
			MainWalk.AnimationId = WalkL.AnimationId
		end

		return Output
	end

end

RunSer.RenderStepped:Connect(function()
	local ForceVector = ToWalkRelativeToHRP(Humanoid, Humanoid.MoveDirection, HumanoidRoot)
	print(ForceVector)

	if ForceVector == "Forwards" then
		MainWalk.AnimationId = WalkF.AnimationId
		print(MainWalk.AnimationId)
	elseif ForceVector == "Backwards" then
		MainWalk.AnimationId = WalkB.AnimationId
		print(MainWalk.AnimationId)
	elseif ForceVector == "Right" then
		MainWalk.AnimationId = WalkR.AnimationId
		print(MainWalk.AnimationId)
	elseif ForceVector == "Left" then
		MainWalk.AnimationId = WalkL.AnimationId
		print(MainWalk.AnimationId)
	end

end)

The setup of the Animations script in Explorer
image

TL;DR - The walking animations won’t play, but will stay as a still animation (no movement), the script changes walking id yet still dont play dispite being animated

This is a common problem with changing the animation id while it’s still playing. Recommendation, when you change the animation id you also have to stop the current animation or fade it out and play the new animation.

I made that post, but, I don’t think I said this in my previous post.
Tell me if you have problems finding how to fix it, I’m sure I could come in handy.

Keep in mind that you can make a better script for player movement change than this, this script has trouble smoothing out between animations.

You can try a different approach. I was working on this recently, and at the end I decided to change the legs rotation instead of doing 8 animations. Here’s the script:

local root = script.Parent.HumanoidRootPart

local valueX
local valueY
local valueZ

local valuedirection = 1

local LlegCrame = script.Parent.Torso["Left Hip"].C0
local RlegCrame = script.Parent.Torso["Right Hip"].C0

local rootCFrame = root["RootJoint"].C1

TorsoCFrame = script.Parent.Torso.CFrame

local Lrotation = script.Parent.Torso["Left Hip"].C0
local Lrotationleft = script.Parent.Torso["Left Hip"].C0 * CFrame.Angles(0, math.rad(70), 0)
local Lrotationright = script.Parent.Torso["Left Hip"].C0 * CFrame.Angles(0, math.rad(-70), 0)

local Rrotation = script.Parent.Torso["Right Hip"].C0
local Rrotationleft = script.Parent.Torso["Right Hip"].C0 * CFrame.Angles(0, math.rad(70), 0)
local Rrotationright = script.Parent.Torso["Right Hip"].C0 * CFrame.Angles(0, math.rad(-70), 0)

game["Run Service"].RenderStepped:Connect(function()
local relativeVelocity = (root.CFrame - root.Position):Inverse() * Vector3.new(root.AssemblyLinearVelocity.X, root.AssemblyLinearVelocity.Y, root.AssemblyLinearVelocity.Z)
valueX = math.round(relativeVelocity.X)/16
valueY = math.round(relativeVelocity.Y)/16
valueZ = math.round(relativeVelocity.Z)/16
--print(valueX, valueZ)
--print(root.AssemblyLinearVelocity)
if valueZ < -.1 then --front
valuedirection = 1
end
if valueZ > .1 then --back
valuedirection = -1
end
if valueX == 0 then
local XvalueDirection = 0
end
if not (script.Parent.Torso["Left Hip"].C0 == LlegCrame * CFrame.Angles(0, -math.rad(valueX*60*valuedirection), 0)) then
local tweenService = game["TweenService"]
local tweenInfo = TweenInfo.new(
.2,
Enum.EasingStyle.Linear,
Enum.EasingDirection.In,
0,
false,
0
)
local LGoal = {
C0 = LlegCrame * CFrame.Angles(0, -math.rad(valueX*40*valuedirection), 0)
}
local Ltween = tweenService:Create(script.Parent.Torso["Left Hip"], tweenInfo, LGoal)
local RGoal = {
C0 = RlegCrame * CFrame.Angles(0, -math.rad(valueX*40)*valuedirection, 0)
}

local Rtween = tweenService:Create(script.Parent.Torso["Right Hip"], tweenInfo, RGoal)
local RootGoal = {
C1 = rootCFrame * CFrame.Angles(0, 0, math.rad(valueX*40*valuedirection))
}
local Roottween = tweenService:Create(script.Parent.HumanoidRootPart["RootJoint"], tweenInfo, RootGoal)
Ltween:Play()
Rtween:Play()
Roottween:Play()
--script.Parent.Torso["Left Hip"].C0 = LlegCrame * CFrame.Angles(0, -math.rad(valueX*30*valuedirection), 0)
--script.Parent.Torso["Right Hip"].C0 = RlegCrame * CFrame.Angles(0, -math.rad(valueX*30)*valuedirection, 0)
end
end)

Note: Put this LocalScript in StarterCharacterScripts.
This script is client sided. (ill get into making it server sided later)

The only thing you should do is add a different animation when walking forward and another when walking backwards

if valueZ < -.1 then --front
valuedirection = 1
end
if valueZ > .1 then --back
valuedirection = -1
end

Hope it helps!

2 Likes

Hello, your script doesn’t work if you press more than one key at a time. do you know a solution?

Im sorry for my late response havent really been active on the devforum since then. Regarding ur question, i havent experienced any issue with pressing multiple keys at the same time, the only issue are some errors on death.