Running animation continues playing even while standing still

  1. What do you want to achieve? Keep it simple and clear!

I want the animation that plays while you are sprinting to stop playing when you start standing still.

  1. What is the issue? Include screenshots / videos if possible!

When I let go of the movement keys while still holding down the Sprint key, the running animation continues playing. The player’s WalkSpeed doesn’t change back to 16 either, but it doesn’t matter as you aren’t moving.

Another problem is that the jumping animation doesn’t play while the player is sprinting, though it could be because of animation priorities.

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

I’ve tried many things such as checking if the player has stopped moving, but I believe the problem is that I am using ContextActionService. I am pretty new to scripting but the reason I’m using ContextActionService is because I am trying to add mobile support. There could be better ways of doing it, so please tell me if so.

local contextactionService = game:GetService("ContextActionService")
local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local human = char:WaitForChild("Humanoid")
local animator = human:FindFirstChildOfClass("Animator")

local anim = Instance.new('Animation', animator)
anim.AnimationId = 'rbxassetid://6884452825'
local playAnim = animator:LoadAnimation(anim)

local function ProcessAction(ActionName,InputState,InputObject)
	if ActionName == "Sprint" then
		if InputState == Enum.UserInputState.Begin then
			if char.Humanoid.MoveDirection.Magnitude > 0 then
				char.Humanoid.WalkSpeed = 24
				playAnim:Play()
			else
				playAnim:Stop()
			end
		elseif InputState == Enum.UserInputState.End then
			char.Humanoid.WalkSpeed = 16
			playAnim:Stop()
		end
	end
end

contextactionService:BindAction("Sprint", ProcessAction, true, Enum.KeyCode.LeftShift)
contextactionService:SetPosition("Sprint", UDim2.new(0.72, -65, 0.20, -8))
contextactionService:SetImage("Sprint", "http://www.roblox.com/asset/?id=6856612770")
6 Likes

Instead of playing the animation on the humanoid, you can playtest the game, go inside your character through the explorer and copy the script named “Animate”. Once you stop playing paste it somewhere, open the script and replace the walking and jumping anim ids with yours. Now when the player joins, clone this script into their character

I don’t see how this will solve this, but I’ll try it and let you know.

Unless you’re talking about the jump animation problem I mentioned, then I don’t actually have a custom jump animation. I just want it to play the default Roblox jump animation.

EDIT: Yea, it doesn’t work. I realized that I already have a script that does this anyways, so yeah.

I think your problem is you’re only detecting when the sprint key is first pressed, so when you let go nothing happens. Try making another function for when it’s no longer being held down, or could you could try using this: UserInputService | Roblox Creator Documentation

Edit:
Yes you technically are checking, but in the same function so unless you’re checking for change state it’s only going to go once. So try making the .end it’s own thing.

1 Like

The problem is still happening, but I’ve made the code more efficient.
I’ve found out that I have to make it start constantly checking if the movement keys are down as the problem is that it’s not, but I might be wrong.

Here’s my code so far:

local function movementKeys()
	return userinputService:IsKeyDown(Enum.KeyCode.W) or userinputService:IsKeyDown(Enum.KeyCode.A) or userinputService:IsKeyDown(Enum.KeyCode.S) or userinputService:IsKeyDown(Enum.KeyCode.D)
end

local function processAction(action, state, input)
	if action == "Sprint" then
		if input.UserInputType == Enum.UserInputType.Keyboard then
			if state == Enum.UserInputState.Begin then
				char.Humanoid.WalkSpeed = 24
				if not movementKeys() then
					playAnim:Stop()
				else
					playAnim:Play()
				end
			elseif state == Enum.UserInputState.End then 
				char.Humanoid.WalkSpeed = 16
				playAnim:Stop()
			end
		end
		-- elseif input.UserInputType == Enum.UserInputType.Touch then end
	end
end

Well does it do anything right now, still just not stopping when you let go, or does it do nothing now?

Because I’m not sure if that’s your entire script, but unlike your last script there is nothing causing this function to run.

It does the same thing as before, as I said I just made it more “efficient”.

Because I’m not sure if that’s your entire script, but unlike your last script there is nothing causing this function to run.

Sorry, should’ve said that I renamed a bunch of things and I left out the rest of the script so it was easier to read.

1 Like

I’m on my phone, but do you mind if I try making some changes to your script that should make it work rather than breaking it apart? I might have an idea on how to accomplish what you want.

Edit: I’m sure the formatting on this is horrible but here is something you could try:

`local UIS = game:GetService("UserInputService")
local players = game:GetService("Players")


local player = players.LocalPlayer

local function UIS.InputBegan:Connect(input, gameProcessed)
    if not gameProcessed then        
        if input.UserInputType == Enum.UserInputType.Keyboard then
            local key = input.KeyCode
            if key == Enum.KeyCode.W or key == Enum.KeyCode.A or key == Enum.KeyCode.S or key == Enum.KeyCode.D then 
                player.Character.Humanoid.WalkSpeed = 24
playAnim:Play()
            end
        end
    end
end

local function UIS.InputEnded:Connect(input, gameProcessed)
    if not gameProcessed then
        if input.UserInputType == Enum.UserInputType.Keyboard then
            local key = input.KeyCode
             if key == Enum.KeyCode.W or key == Enum.KeyCode.A or key == Enum.KeyCode.S or key == Enum.KeyCode.D then
                player.Character.Humanoid.WalkSpeed = 16
playAnim:Stop()
            end
        end
    end
end`

Edit2: There are also a lot of videos on YouTube about user input service, and specifically “shift to sprint” videos. You could always take one of those and just add your line of code toggling the animation.

2 Likes

Alright, here’s the rbxm.

RunScript.rbxm (2.1 KB)

Make sure to change the animation ID to one of your own or by Roblox, and put the script into StarterCharacterScripts.

1 Like

I probably should have sent my code in a separate message but I just edited my one above so check that one out, my bad. If that still doesn’t help I’ll definitely check out the place file directly (sometime tomorrow) and see what I can do.

1 Like

Yeah, it doesn’t help since I’m trying to make a mobile button out of it and you can only enter one function into the ContextActionService:BindAction().

I can wait until tomorrow, unless someone else comes and beats you to solving the problem. I’ll just use my old script for now.

You could always add on your code for your mobile button after the code for keyboard.

I don’t think that’s how that works.

The code above should work for anyone using a keyboard. You should be able to make a separate script or add it within the existing script depending on how you want your mobile button to work/what it will be. I’m not exactly sure why you’re set on using ContextActionService. Which is not something I’m very confident using, much less advising on how to use. So if you insist on using it hopefully someone who’s better versed with it will find this topic.

I’m not exactly sure why you’re set on using ContextActionService.

Isn’t ContextActionService how you’re supposed to make mobile buttons? I might be wrong but there’s even a whole page on it on the Developer Hub: Creating Mobile Buttons
Not trying to discredit you, I think you might know what you’re saying, but still.

Are you suggesting to add GUIs instead? If so, wouldn’t it show on other platforms as well? (which I don’t want)

Check the player’s velocity to see if they’re idle or not.

if HumanoidRootPart.Velocity.X == 0 and HumanoidRootPart.Velocity.Z == 0 then
		-- stop the sprinting and the animation
	else
        --- code here

Or you could use check the state of the player with Humanoid:GetState(), there’s some useful code in this article that might help you.

As for the jumping, change the animation priority.

1 Like

Or you could use check the state of the player with Humanoid:GetState(), there’s some useful code in this article that might help you.

Thanks, I’ll try that out to see if it works. Seems promising.

As for the jumping, change the animation priority.

I’ve tried changing the running animation’s priority to Idle, Movement, and Action and none of them let the default Roblox jumping animation play whenever I jumped while running.

1 Like

Movement is the best animation priority for running, to get the jump to work, just use :GetState(), and only call the :Play() function if the player is moving.

2 Likes

Here’s the updated and working code:

local userinputService = game:GetService("UserInputService")
local contextactionService = game:GetService("ContextActionService")
local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local human = char:WaitForChild("Humanoid")
local animator = human:FindFirstChildOfClass("Animator")
local isRunning = false

local anim = Instance.new('Animation', animator)
anim.AnimationId = 'rbxassetid://3180296814'
local playAnim = animator:LoadAnimation(anim)

local function movementKeys()
	return userinputService:IsKeyDown(Enum.KeyCode.W) or userinputService:IsKeyDown(Enum.KeyCode.A) or userinputService:IsKeyDown(Enum.KeyCode.S) or userinputService:IsKeyDown(Enum.KeyCode.D)
end

local function processAction(action, state, input)
	if action == "Sprint" then
		if input.UserInputType == Enum.UserInputType.Keyboard then
			if state == Enum.UserInputState.Begin then
				if movementKeys() then
					char.Humanoid.WalkSpeed = 24
					playAnim:Play()
					isRunning = true
				elseif not movementKeys() then
					char.Humanoid.WalkSpeed = 16
					playAnim:Stop()
					isRunning = false
				end
			elseif state == Enum.UserInputState.End then 
				char.Humanoid.WalkSpeed = 16
				playAnim:Stop()
				isRunning = false
			end
		end
	end
end

   local function Movement()		
	if isRunning == true then 
		if human.MoveDirection ~= Vector3.new(0, 0, 0) then --Still moving
			
		else
			char.Humanoid.WalkSpeed = 16
			playAnim:Stop()
			isRunning = false
		end	
	end
end

contextactionService:BindAction("Sprint", processAction, true, Enum.KeyCode.LeftShift)
contextactionService:SetPosition("Sprint", UDim2.new(0.72, -65, 0.20, -8))
contextactionService:SetImage("Sprint", "http://www.roblox.com/asset/?id=6856612770")
human:GetPropertyChangedSignal("MoveDirection"):Connect(Movement)

Just had to add a check for when the player stops moving to end the animation and reset their walk speed.

7 Likes