Movement System Animations Not working

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    "I want to fix this annoying bug where, when I’m running and I start sliding, the animation gets glitched. The same issue occurs when I press the Esc key.
  2. What is the issue? Include screenshots / videos if possible!

Case A(When I click Esc while Running):

Case BWhen I’m running and then slide, I keep holding down the ‘R’ key while sliding. However, when the slide animation finishes, I release the ‘R’ key.):

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

Yea I tried to find a solution inside the DeveloperForum but I couldn’t find something.

function KeyCodeModuleScript:LoadSmoothAnimation(animation, shouldPlay, seconds)
	if SlideCooldown == true then return end
	print("LoadSmoothAnimation was called", animation, "shouldPlay:", shouldPlay)
	local currentAnimTrack = Animator:LoadAnimation(animation)
	local transitionTime = 1.5

	if shouldPlay == true then
		print("Starting Animation")
		currentAnimTrack:Play(transitionTime, 0)
		currentAnimTrack:AdjustWeight(1, 0.5)
		currentAnimationTrack = currentAnimTrack
	else
		if shouldPlay == false then
			print("Stopping Animation")
			currentAnimationTrack:AdjustWeight(0.5, 1)
			currentAnimationTrack:Stop(transitionTime, 0)
			currentAnimationTrack = nil
		end
	end

	if seconds ~= nil then
		local value = KeyCodeModuleScript.HipHeight
		print("Starting Animation with timer")
		currentAnimTrack:Play(transitionTime, 0)
		currentAnimTrack:AdjustWeight(1, 0.5)
		currentAnimationTrack = currentAnimTrack

		Humanoid.AutoRotate = false
		Humanoid.HipHeight = 0.8
		shouldMove = true
		task.spawn(function()
			moveInDirection(2.5, 50)
			
			task.wait(5)
			SlideCooldown = false
		end)
		Signal.FireServer("KeyCodeModuleScript:VFX", Character, value)
		task.wait(2.5)
		currentAnimTrack:AdjustWeight(0.5, 1)
		SlideCooldown = true
		currentAnimTrack:Stop(0.5, 0)
		Humanoid.AutoRotate = true
		Humanoid.HipHeight = value
		currentAnimationTrack = nil
	end
end


function KeyCodeModuleScript:PerformMovements(Keys, Type)
	print("Key:", Keys, "Type:", Type)
	if Keys == "ValueR" and Type == "InputBegan" then
		self:LoadSmoothAnimation(runAnimation, true, nil)
		startSprint()
	elseif Keys == "ValueR" and Type == "InputEnded" then
		stopSprint()
		self:LoadSmoothAnimation(runAnimation, false, nil)
		
	elseif Keys == "ValueC" and Type == "InputBegan" and Humanoid:GetState() ~= (Enum.HumanoidStateType.FallingDown and Enum.HumanoidStateType.Freefall) then
		self:LoadSmoothAnimation(slideAnimation, true, 2)
		if isSprinting then
			stopSprint()
			
		end
		
	elseif Keys == "OtherValue"	then
		
	end
end
2 Likes

My videos aren’t working very well so I will be more descriptive.

On the Case A:

While I’m pressing the key R to run I pressed the key C to slide. When the slide movement finishes and I top pressing the key R, the run animation doesn’t stop.

On the Case B.

While I’m pressing the key R, and suddenly I press the key ESC, the animation still gets stuck

Can someone help me to find a solution to the issue, please?

Is there a reason you aren’t using UIS (Userinputservice) that already has solutions to both these problems?

The problem with R to C key is probably due to the fact the method you’re using to detect inputs can only focus on one key press at a time.

The problem with ESC key is most likely due to the fact of the same problem above ^ or the fact ESC is a game process which causes this bug and is common in other games, a simple fix is too just stop the run animation whenever the players movement is completely still.

If you don’t want to use userinputservice then try to not use elseif statements and just use individual if statements.

1 Like

I’m using UserInputService on the Start Function, for organization purposes, as well as context action service for mobile:

function KeyCodeModuleScript:Start()
	UIS.InputBegan:Connect(function(input, gameProcessed)
		local Keys, Type = self:DetectKeysPressed(input, gameProcessed, true)
		if Keys then
			self:PerformMovements(Keys, Type)
		end
	end)
	UIS.InputEnded:Connect(function(input, gameProcessed)
		local Keys, Type = self:DetectKeysPressed(input, gameProcessed, false)
		if Keys then
			self:PerformMovements(Keys, Type)
		end
	end)

	-- Bind actions to buttons
	CAS:BindAction("SprintButton", function(actionName, inputState)
		local sprintButton = CAS:GetButton(actionName)
		if inputState == Enum.UserInputState.Begin then
			self:LoadSmoothAnimation(runAnimation, true, nil)
			startSprint()
	
		elseif inputState == Enum.UserInputState.End then
			stopSprint()
			self:LoadSmoothAnimation(runAnimation, false, nil)
			if sprintButton then
				sprintButton.Image = "rbxassetid://17628646556" -- Reset image in case it changed
			end
		end
	end, true, Enum.KeyCode.Unknown)

	CAS:BindAction("SlideButton", function(actionName, inputState)
		local slideButton = CAS:GetButton(actionName)
		if inputState == Enum.UserInputState.Begin then
			self:LoadSmoothAnimation(slideAnimation, true, 2)
			if slideButton then
				slideButton.Image = "rbxassetid://17628643479" -- Reset image in case it changed
			end
		end
	end, true, Enum.KeyCode.Unknown)

	-- Get the buttons created by ContextActionService
	local sprintButton = CAS:GetButton("SprintButton")
	local slideButton = CAS:GetButton("SlideButton")
	CAS:SetImage("SprintButton", "rbxassetid://17628646556")
	CAS:SetPosition("SprintButton", UDim2.new(-0.4,0,0.3,0))
	CAS:SetImage("SlideButton", "rbxassetid://17628643479")
	CAS:SetPosition("SlideButton", UDim2.new(0.2,0,0,0))


   if sprintButton then
		sprintButton.Size = UDim2.new(0,125,0,125)
		sprintButton.ActionIcon.Size = UDim2.new(0,200,0,200)
        local UIAspectRatio = Instance.new("UIAspectRatioConstraint")
		UIAspectRatio.Parent = sprintButton
		sprintButton.ActionIcon.Position = UDim2.new(-0.3,0,-0.3, 0)
   end
   
	if slideButton then
	
		slideButton.Size = UDim2.new(0,125,0,125)
		slideButton.ActionIcon.Size = UDim2.new(0,200,0,200)
		local UIAspectRatio = Instance.new("UIAspectRatioConstraint")
		UIAspectRatio.Parent = slideButton
		slideButton.ActionIcon.Position = UDim2.new(-0.3,0,-0.3, 0)
		
		task.spawn(function()
			while wait(0.2) do
				if SlideCooldown == true then
					CAS:SetImage("SlideButton", "rbxassetid://17664293375")	
				else
					CAS:SetImage("SlideButton", "rbxassetid://17628643479")
				
				end
			end
		end)
	end
end

Should I remove those from the function, or what is best for the code?

1 Like

Try something like this

function KeyCodeModuleScript:PerformMovements(Keys, Type)
	print("Key:", Keys, "Type:", Type)
	if Keys == "ValueR" and Type == "InputBegan" then
		self:LoadSmoothAnimation(runAnimation, true, nil)
		startSprint()
       return
    end
	if Keys == "ValueR" and Type == "InputEnded" then
		stopSprint()
		self:LoadSmoothAnimation(runAnimation, false, nil)
        return
    end	
	if Keys == "ValueC" and Type == "InputBegan" and Humanoid:GetState() ~= (Enum.HumanoidStateType.FallingDown and Enum.HumanoidStateType.Freefall) then
		self:LoadSmoothAnimation(slideAnimation, true, 2)
		if isSprinting then
			stopSprint()
		end
        return
	end
	if Keys == "OtherValue"	then
       
  return
	end
end
1 Like

Also remove this

if SlideCooldown == true then return end

You’re stopping any animations if the slidecooldown is active.

1 Like

I didn’t saw that message XD, wait a minute

It kind of worked, but it gave me an error message on line, also how can I remove this


:

currentAnimationTrack:AdjustWeight(0.5, 1)

Error:
KeyCodeModuleScript:130: attempt to index nil with ‘AdjustWeight’

if SlideCooldown == true then return end
if seconds ~= nil then
		local value = KeyCodeModuleScript.HipHeight
		print("Starting Animation with timer")
		currentAnimTrack:Play(transitionTime, 0)
		currentAnimTrack:AdjustWeight(1, 0.5)
		currentAnimationTrack = currentAnimTrack

		Humanoid.AutoRotate = false
		Humanoid.HipHeight = 0.8
		shouldMove = true
		task.spawn(function()
			moveInDirection(2.5, 50)
			
			task.wait(5)
			SlideCooldown = false
		end)
		Signal.FireServer("KeyCodeModuleScript:VFX", Character, value)
		task.wait(2.5)
		currentAnimTrack:AdjustWeight(0.5, 1)
		SlideCooldown = true
		currentAnimTrack:Stop(0.5, 0)
		Humanoid.AutoRotate = true
		Humanoid.HipHeight = value
		currentAnimationTrack = nil
	end

All of this seems to be for sliding, I would have this as a separate function and not in the function that controls your animations universally.

If you want to know a tip that makes movement systems easy to deal with at least for me, is to use boolean values. One for sliding, running, crouching, etc. then you just check when those values are changed with

BoolValue.Changed:Connect(function()
if BoolValue.Value == true then
Run:Play()
else
Run:Stop
end
end)

This way all you have to do is switch a bool value to true or false when the key is pressed, it also works well since you can just do the same thing with mobile.

1 Like

You are right. Should I divide the function into two parts, one for sliding and another for running?

Also, thanks for the advice. I will integrate it into the code.

I decided to divide completely into 2 parts but I have the same error:

local function StartSlideAnimation(seconds, animation)
	local currentAnimTrack = Animator:LoadAnimation(animation)
	local transitionTime = 1.5
	if SlideCooldown == true then return end
	if seconds ~= nil then
		local value = KeyCodeModuleScript.HipHeight
		print("Starting Animation with timer")
		currentAnimTrack:Play(transitionTime, 0)
		currentAnimTrack:AdjustWeight(1, 0.5)
		currentAnimationTrack = currentAnimTrack

		Humanoid.AutoRotate = false
		Humanoid.HipHeight = 0.8
		shouldMove = true
		task.spawn(function()
			moveInDirection(2.5, 50)

			task.wait(5)
			SlideCooldown = false
		end)
		Signal.FireServer("KeyCodeModuleScript:VFX", Character, value)
		task.wait(2.5)
		currentAnimTrack:AdjustWeight(0.5, 1)
		SlideCooldown = true
		currentAnimTrack:Stop(0.5, 0)
		Humanoid.AutoRotate = true
		Humanoid.HipHeight = value
		currentAnimationTrack = nil
	end
end
function KeyCodeModuleScript:LoadSmoothAnimation(animation, shouldPlay, seconds)
	print("LoadSmoothAnimation was called", animation, "shouldPlay:", shouldPlay)
	local currentAnimTrack = Animator:LoadAnimation(animation)
	local transitionTime = 1.5

	if shouldPlay == true then
		print("Starting Animation")
		currentAnimTrack:Play(transitionTime, 0)
		currentAnimTrack:AdjustWeight(1, 0.5)
		currentAnimationTrack = currentAnimTrack
	else
		if shouldPlay == false then
			print("Stopping Animation")
			currentAnimationTrack:AdjustWeight(0.5, 1)
			currentAnimationTrack:Stop(transitionTime, 0)
			currentAnimationTrack = nil
		end
	end
end


function KeyCodeModuleScript:PerformMovements(Keys, Type)
	print("Key:", Keys, "Type:", Type)
	if Keys == "ValueR" and Type == "InputBegan" then
		self:LoadSmoothAnimation(runAnimation, true, nil)
		startSprint()
		return
	end
	if Keys == "ValueR" and Type == "InputEnded" then
		stopSprint()
		self:LoadSmoothAnimation(runAnimation, false, nil)
		return
	end	
	if Keys == "ValueC" and Type == "InputBegan" and Humanoid:GetState() ~= (Enum.HumanoidStateType.FallingDown and Enum.HumanoidStateType.Freefall) then
		StartSlideAnimation(2,slideAnimation)
		if isSprinting then
			stopSprint()
		end
		return
	end
	if Keys == "OtherValue"	then

		return
	end
end

Sadly I still have the same error, what other solutions should we try?

What other solutions should we try?

Can someone help me to solve this issue, please?

Please?, I’m really confused about.

your videos aren’t loading, they’re just stuck at 00:00

1 Like

Yea, I’m very sorry about that, my computer is not working correctly on that aspect. But I wrote the issue in the second comment, which is this, Plus some images about the issue:

My videos aren’t working very well so I will be more descriptive.

On the Case A:

While I’m pressing the key R to run I pressed the key C to slide. When the slide movement finishes and I top pressing the key R, the run animation doesn’t stop.

Extra:Case C.

While I’m sliding and I run and unhold the C button this strange bug Happens

it is stuck in this position, literally

On the Case B.

While I’m pressing the key R, and suddenly I press the key ESC, the animation still gets stuck

Image Before Sliding(Holding the R key):

it is just stuck on the run animation even if i close the menu.

Is it okay the information I gave, or is there any other information I could provide to fix the bug?

@absurf_gpo or @StraightScared do you think the information that I gave was alright or do I give more?