How do I use the Run Animation from the Animate Script?

This is a support category for asking questions about how to get something done on the Roblox websites or how to do something on Roblox applications such as Roblox Studio.

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 make a running system that uses the running animation instead of walking animation

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

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I put an Animate script with my animations inside the StarterCharacterScripts and I made this LocalScript also inside StarterCharacterScripts:

-- Services --
local contextActionService = game:GetService("ContextActionService")
local runService = game:GetService("RunService")
local players = game:GetService("Players")

-- Instances --
local player = players.LocalPlayer
local character = player.Character
local humanoid = character:FindFirstChildOfClass("Humanoid")
local animator: Animator = humanoid:WaitForChild("Animator")

local runAnimationTrack = animator:LoadAnimation(script.RunAnim)
runAnimationTrack.Looped = true
runAnimationTrack.Priority = Enum.AnimationPriority.Movement

-- Constants --
local RUN_ACTION_NAME = "Run"

-- Variables --
local isRunning = false

local function RunHandler(ActionName: string, InputState: Enum.UserInputState)
	if ActionName ~= RUN_ACTION_NAME or InputState ~= Enum.UserInputState.Begin then
		return
	end
	
	isRunning = not isRunning
	
	if isRunning then
		humanoid.WalkSpeed = 32
		runAnimationTrack:Play()
	else
		humanoid.WalkSpeed = 16
		runAnimationTrack:Stop()
	end
end

contextActionService:BindAction(RUN_ACTION_NAME, RunHandler, false, Enum.KeyCode.LeftControl)

But it overrides the idle and jump animations. I just want like, instead of using the walk animation, it uses the run Animation. Is that possible?

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

1 Like

You’re code is only listening to when the key is pressed
InputState ~= Enum.UserInputState.Begin
You also need to listen to when the key is released.
So it should somewhat be like this :

if InputState == Enum.UserInputState.Begin then
		humanoid.WalkSpeed = 32
		runAnimationTrack:Play()
	elseif InputState == Enum.UserInputState.End then
		humanoid.WalkSpeed = 16
		runAnimationTrack:Stop()
	end

If you dont want the animation to play when player jumps then you need to listen to the Humnoid’s state. i.e, when it is jumping or when it is in air you need to stop animation. when the input is still pressed and the humonoid is on the ground, then play the animation again.

Thanks for pointing it. But I wanna make the running like a switch, where you press Ctrl once and your character starts running, and if you click it again, your character stop running and walk again. Kinda like how The Strongest Battlegrounds do.

1 Like

For anyone who might have this problem in the future, I kinda solved it by modifying the default Animate script at the OnRunning function. Here is the modified code for anyone who wants to use it:

function onRunning(speed)
	speed /= getRigScale()
	
	if speed > 16 then
		playAnimation("run", 0.1, Humanoid)
		pose = "Running"
	elseif speed <= 16 and speed > 0.01 then
		playAnimation("walk", 0.1, Humanoid)
		if currentAnimInstance and currentAnimInstance.AnimationId == "http://www.roblox.com/asset/?id=180426354" then
			setAnimationSpeed(speed / 14.5)
		end
		pose = "Walking"
	else
		if emoteNames[currentAnim] == nil then
			playAnimation("idle", 0.1, Humanoid)
			pose = "Standing"
		end
	end
end