Shift to sprint - How to change the player's animation when running

Hello,

Me and a friend are currently building a game and I would like to implement some sort of shift to sprint with a custom animation. Currently I’ve figured out how to do a shift to sprint script which simply increases the player’s walkspeed to ~30, I’ve also made an animation (Here: https://create.roblox.com/marketplace/asset/13510270711) with Moon Animator (priority set to action).

However I have not been able to figure out how to make this animation play… well at all really, I can’t get it to play while holding shift, i cant get it to play while holding shift and moving, it just doesn’t work. I’ve checked the Roblox Creator API and couldn’t figure it out.

I checked the devforum and found this post which seemed promising but didn’t seem to work (it has it’s own shift to sprint script which didnt work (and didn’t work in conjunction to mine)).

Any help is appreciated thank you!

Clown.

Insert an animation into the local script and set the animation id to 13510270711, then in your script do this

local Animation = script:WaitForChild("YourAnimationName")
local AnimTrack = Humanoid:LoadAnimation(Animation)

-- In your sprinting part
Animation:Play()

-- When the player stops sprinting do
Animation:Stop()

Seems like it would work but sadly it doesn’t… OH very important note, I’m using @MaximumADHD’s Realism Script, if that could be influencing it then i need to find some way to make both work

heres the script so far

local Players = game:GetService("Players")
local player = Players.LocalPlayer
local Humanoid = player.Character:WaitForChild("Humanoid")
local sprintSpeed = 30
local walkSpeed = 16
local Animation = script:WaitForChild("RunAnim")
local AnimTrack = Humanoid:LoadAnimation(Animation)

local userInput = game:GetService("UserInputService")

local function beginSprint(input, gameProcessed)
	if not gameProcessed then
		if input.KeyCode == Enum.KeyCode.LeftShift then
			player.Character.Humanoid.WalkSpeed = sprintSpeed
			Animation:Play() -- Play the run animation
		end
	end
end

local function endSprint(input, gameProcessed)
	if not gameProcessed then
		if input.KeyCode == Enum.KeyCode.LeftShift then
			player.Character.Humanoid.WalkSpeed = walkSpeed
			Animation:Stop() 
	end
end

userInput.InputBegan:Connect(beginSprint)
userInput.InputEnded:Connect(endSprint)

I am sorry, I made a veryyyyy small error, which breaks it :skull::skull::skull:. Here’s a fixed version of the script

local Character = script.Parent
local Humanoid = Character:WaitForChild("Humanoid")

local DefaultSpeed = game:GetService("StarterPlayer").CharacterWalkSpeed
local SprintSpeed = 30

local Animation = script:WaitForChild("Animation")
local AnimTrack = Humanoid:LoadAnimation(Animation)

local UserInputService = game:GetService("UserInputService")

local SprintKeyBind = Enum.KeyCode.LeftShift

UserInputService.InputBegan:Connect(function(Input, gameProcessed)
	if not gameProcessed then
		if Input.KeyCode == SprintKeyBind then
			Humanoid.WalkSpeed = SprintSpeed
			AnimTrack:Play()
		end
	end	
end)

UserInputService.InputEnded:Connect(function(Input, gameProcessed)
	if not gameProcessed then
		if Input.KeyCode == SprintKeyBind then
			Humanoid.WalkSpeed = DefaultSpeed
			AnimTrack:Stop()
		end
	end	
end)

local ShiftKeyDown = false

game:GetService(“UserInputService”).InputBegan:Connect(function(input, gameProcessedEvent)
if input.KeyCode == Enum.KeyCode.LeftShift then
ShiftKeyDown = true
end
end)

game:GetService(“UserInputService”).InputEnded:Connect(function(input, gameProcessedEvent)
if input.KeyCode == Enum.KeyCode.LeftShift then
ShiftKeyDown = false
end
end)

game:GetService(“RunService”).RenderStepped:Connect(function(deltaTime)
if ShiftKeyDown then
local humanoid = game.Workspace:FindFirstChildOfClass(“Humanoid”)
if humanoid and humanoid.MoveDirection.magnitude > 0 then
local animationTrack = humanoid.AnimationController:LoadAnimation(YourAnimationId)
animationTrack:Play()
end
end
end)

do triple ` and then put lua and then do it again but without the lua part to put the code part
its hard to explain but like this

``lua add another `

and then ```

alright ill try to fix it thanks!

I’ll try these when i can, thank you for the responses

I’m not sure if this is solved. But I would use RunService to change the animation if a variable is true.

This seemingly does not work, Just to confirm, StarterPlayerScripts, Animation object named “Animation” within a LocalScript with the id of 13510270711, No errors in the output, just holding shift doesn’t make the character move any faster and doesn’t change the animation.

Doesn’t work, StarterPlayerScripts, local script. Doesn’t make the character move any faster and doesn’t add the animation, also had to fix some incorrect quotes (quotes instead of apostrophes)

If this is not how your scripts should be utilised please inform me of how I can make it work,

Thanks for trying, it’s appreciated!

that’s because you’re making a script in starterplayerscripts, do it in startercharacterscripts, which puts the script IN the player’s character

Also don’t use Humanoid:LoadAnimation(). It’s deprecated. Do

Humanoid.Animator:LoadAnimation().

Alright, with a bit of tinkering i was able to

  1. Make the animation loop
    and
  2. Make the animation only play if the player is moving
local Character = script.Parent
local Humanoid = Character:WaitForChild("Humanoid")

local DefaultSpeed = game:GetService("StarterPlayer").CharacterWalkSpeed
local SprintSpeed = 30

local Animation = script:WaitForChild("Animation")
local AnimTrack = Humanoid.Animator:LoadAnimation(Animation)

local UserInputService = game:GetService("UserInputService")

local SprintKeyBind = Enum.KeyCode.LeftShift

local isMoving = false

UserInputService.InputBegan:Connect(function(Input, gameProcessed)
	if not gameProcessed then
		if Input.KeyCode == SprintKeyBind then
			Humanoid.WalkSpeed = SprintSpeed
			isMoving = true
		end
	end	
end)

UserInputService.InputEnded:Connect(function(Input, gameProcessed)
	if not gameProcessed then
		if Input.KeyCode == SprintKeyBind then
			Humanoid.WalkSpeed = DefaultSpeed
			isMoving = false
			AnimTrack:Stop()
		end
	end	
end)

Humanoid.Running:Connect(function(speed)
	if speed > 0 and isMoving then
		AnimTrack:Play()
		AnimTrack:AdjustSpeed(1) -- Set the animation speed to 1 (default speed)
		AnimTrack.Looped = true -- Enable looping
	elseif speed == 0 or not isMoving then
		AnimTrack:Stop()
	end
end)

I’m going to give you the solution to the post though, Thanks to everyone for the help!

1 Like