Holding 2 Conflicting Movement Keys Brakes Animations

Making a JJBA inspired game and I want the stand to lean when you are moving and sprinting. For some reason when I hold down conflicting movement keys my “fail-safe” of sorts only brakes the animations. For example, if you are running and hold 2 conflicting keys then the stand’s walking animation brakes. Any idea on how to fix?

local RunService = game:GetService("RunService")

local Player = game.Players.LocalPlayer
local Character = Player.Character
local Humanoid = Character.Humanoid
local uis = game:GetService("UserInputService")

local Stand = script.Parent

local SprintAnim = Stand.AnimationController:LoadAnimation(script:WaitForChild("Run"))
SprintAnim:Play()
local LeanAnim = Stand.AnimationController:LoadAnimation(script:WaitForChild("Lean"))
LeanAnim:Play()

function TwoDown() --my fail safe for 2 movement keys but doesnt work
	if uis:IsKeyDown(Enum.KeyCode.A) and uis:IsKeyDown(Enum.KeyCode.D) or uis:IsKeyDown(Enum.KeyCode.W) and uis:IsKeyDown(Enum.KeyCode.S) then
		return true
	else
		return false
	end
end

RunService.Heartbeat:Connect(function()
	if Humanoid.MoveDirection == Vector3.new(0, 0, 0) then
		if TwoDown() then --making sure player isnt just quickly switching between keys
			wait(.3)
			if TwoDown() then --fail-safe in script
				LeanAnim:AdjustWeight(0)
				SprintAnim:AdjustWeight(0)
			end
		else
			LeanAnim:AdjustWeight(0)
			SprintAnim:AdjustWeight(0)
		end
	elseif Humanoid.MoveDirection ~= Vector3.new(0,0,0) and uis:IsKeyDown(Enum.KeyCode.LeftShift) then --anims for sprinting and walking
		SprintAnim:AdjustWeight(1.001)
		LeanAnim:AdjustWeight(0)
	elseif Humanoid.MoveDirection ~= Vector3.new(0,0,0) then
		LeanAnim:AdjustWeight(1.001)
		SprintAnim:AdjustWeight(0)
	end
end)

Walk anim braking:

Run anim braking:

1 Like

I fixed it! I dont know if the following is accurate, BUT I believe humanoid.MoveDirection is server sided, meaning that if a humanoid is moving is determined by the server, not the client. This can cause issues because a player can by holding two conflicting movement keys (which should cause them to stop moving) but still have humanoid.MoveDirection not equal to (0, 0, 0). For anyone who finds this thread and needs a solution, I changed my script to the following:

local RunService = game:GetService("RunService")

local Player = game.Players.LocalPlayer
local Character = Player.Character
local Humanoid = Character.Humanoid
local uis = game:GetService("UserInputService")

local Stand = script.Parent

local SprintAnim = Stand.AnimationController:LoadAnimation(script:WaitForChild("Run"))
SprintAnim:Play()
local LeanAnim = Stand.AnimationController:LoadAnimation(script:WaitForChild("Lean"))
LeanAnim:Play()

function TwoDown()
	if uis:IsKeyDown(Enum.KeyCode.A) and uis:IsKeyDown(Enum.KeyCode.D) or uis:IsKeyDown(Enum.KeyCode.W) and uis:IsKeyDown(Enum.KeyCode.S) then
		return true
	else
		return false
	end
end

RunService.Heartbeat:Connect(function()
	if TwoDown() then
		if Humanoid.MoveDirection == Vector3.new(0, 0, 0) then
			SprintAnim:AdjustWeight(0)
			LeanAnim:AdjustWeight(0)
		end
	elseif Humanoid.MoveDirection == Vector3.new(0, 0, 0) and not TwoDown() then
		SprintAnim:AdjustWeight(0)
		LeanAnim:AdjustWeight(0)
	elseif Humanoid.MoveDirection ~= Vector3.new(0,0,0) and uis:IsKeyDown(Enum.KeyCode.LeftShift) then
		SprintAnim:AdjustWeight(1.001)
		LeanAnim:AdjustWeight(0)
	elseif Humanoid.MoveDirection ~= Vector3.new(0,0,0) then
		LeanAnim:AdjustWeight(1.001)
		SprintAnim:AdjustWeight(0)
	end
end)

this works better because if the player is moving while having to keys held at the same time it doesnt break itself and just changes nothin. Also it doesnt need to double check if the player still has two keys held down to give the stand the idle anim