How might I play the animation in this Shift to Sprint Script?

I’ve been trying multiple times in order to try to play the animation in this shift to sprint. I’d would also love to make a check in order to stop the animation when the player’s movement is halted.

Here is my script:

local PlayersService = game:GetService("Players")
local UIS = game:GetService("UserInputService")


local playerEntity = PlayersService.LocalPlayer
local character = playerEntity.Character or playerEntity.CharacterAdded:Wait()
local characterHumanoid = character:WaitForChild("Humanoid")

local normalSpeed = 16
local boostedSpeed = normalSpeed * 1.25

local function startSprinting(key)
	if key.KeyCode == Enum.KeyCode.LeftShift then
		characterHumanoid.WalkSpeed = boostedSpeed
		local animation = Instance.new("Animation")
		animation.AnimationId = "http://www.roblox.com/asset/?id=9360403056"
	end
end

local function stopSprinting(key)
	if key.KeyCode == Enum.KeyCode.LeftShift then
		characterHumanoid.WalkSpeed = normalSpeed
	end
end

UIS.InputBegan:Connect(startSprinting)
UIS.InputEnded:Connect(stopSprinting)

Load the animation into the animator which is found inside of the humanoid.

local humanoid = character:WaitForChild("Humanoid")
local Animator = humanoid:WaitForChild("Animator")

local Anim = Animator:LoadAnimation(animation)

Anim:Play() -- To Play

Anim:Stop()-- To Stop

Important

Do not LoadAnimation every time they sprint, instead make it when the script runs.

Then, you can just play or stop it.

Would i put this in between startsprinting? or before or after?

You want to load the animation as a varible, and play the animation inside of the functions.

You should use contextactionservice instead of userinputservice because userinputservice triggers while you’re typing and you can’t make mobile buttons for it.

You need to create the animation with instance.new by the way. You also need to change its id. So you basically do:

local animation = Instance.new("Animation", Humanoid)
animation.AnimationId = "your animation ID"

I used this to load the animation, but it didn’t work.

Nevermind, it was an error with the variables.

like this?

local PlayersService = game:GetService("Players")
local UIS = game:GetService("UserInputService")
local animation = Instance.new("Animation")
animation.AnimationId = "http://www.roblox.com/asset/?id=9360403056"


local playerEntity = PlayersService.LocalPlayer
local character = playerEntity.Character or playerEntity.CharacterAdded:Wait()
local characterHumanoid = character:WaitForChild("Humanoid")

local normalSpeed = 16
local boostedSpeed = normalSpeed * 1.25

local function startSprinting(key)
	if key.KeyCode == Enum.KeyCode.LeftShift then
		characterHumanoid.WalkSpeed = boostedSpeed
		local Anim = animation:LoadAnimation(animation)
		Anim:Play() -- To Play
		Anim:Stop()-- To Stop
	end
end

local function stopSprinting(key)
	if key.KeyCode == Enum.KeyCode.LeftShift then
		characterHumanoid.WalkSpeed = normalSpeed
	end
end

UIS.InputBegan:Connect(startSprinting)
UIS.InputEnded:Connect(stopSprinting)

No. That is gonna both play and stop. You need InputBegan and InputEnded which you should used in your original script to change walk speed. Also please, use contextactionservice. It’s better than userinputservice. You won’t have to rewrite much.

I only know how to use uis, not cts.
I’m also not familiar with InputBegan or InputEnded, do you mind explaining? (I’m pretty new to lua, so it’s a bit difficult to understand everything.)

Like this

local PlayersService = game:GetService("Players")
local UIS = game:GetService("UserInputService")
local animation = Instance.new("Animation")
animation.AnimationId = "http://www.roblox.com/asset/?id=9360403056"


local playerEntity = PlayersService.LocalPlayer
local character = playerEntity.Character or playerEntity.CharacterAdded:Wait()
local characterHumanoid = character:WaitForChild("Humanoid")

local normalSpeed = 16
local boostedSpeed = normalSpeed * 1.25

local Anim = animation:LoadAnimation(animation)

local function startSprinting(key)
	if key.KeyCode == Enum.KeyCode.LeftShift then
		characterHumanoid.WalkSpeed = boostedSpeed
		Anim:Play() -- To Play
	end
end

local function stopSprinting(key)
	if key.KeyCode == Enum.KeyCode.LeftShift then
		characterHumanoid.WalkSpeed = normalSpeed
        Anim:Stop()-- To Stop
	end
end

UIS.InputBegan:Connect(startSprinting)
UIS.InputEnded:Connect(stopSprinting)

I put this into the script, and it doesn’t function. The script no longer works with this.

Binding:

CAS:BindAction("Run", starts printing, (boolean if you want roblox to create a button for mobile), "LeftShift")

You can also use:

local button = CAS:GetButton()

To make a variable of the mobile button.

Oh, because you are not using the animator… inside the humanoid…

Try this

local PlayersService = game:GetService("Players")
local UIS = game:GetService("UserInputService")
local animation = Instance.new("Animation")
animation.AnimationId = "http://www.roblox.com/asset/?id=9360403056"


local playerEntity = PlayersService.LocalPlayer
local character = playerEntity.Character or playerEntity.CharacterAdded:Wait()
local characterHumanoid = character:WaitForChild("Humanoid")

local normalSpeed = 16
local boostedSpeed = normalSpeed * 1.25

local Anim = characterHumanoid:WaitForChild("Animator"):LoadAnimation(animation)

local function startSprinting(key)
	if key.KeyCode == Enum.KeyCode.LeftShift then
		characterHumanoid.WalkSpeed = boostedSpeed
		Anim:Play() -- To Play
	end
end

local function stopSprinting(key)
	if key.KeyCode == Enum.KeyCode.LeftShift then
		characterHumanoid.WalkSpeed = normalSpeed
        Anim:Stop()-- To Stop
	end
end

UIS.InputBegan:Connect(startSprinting)
UIS.InputEnded:Connect(stopSprinting)
1 Like

It now works! However, when you come to a full stop, the animation does not stop playing. I assume that I must do the following code:

Humanoid.MoveDirection.magnitude == 0

where might I put that in my script as well?

Pretty sure it’s gonna error. I’m pretty sure magnitude is NAN (not a number)

I’m confused by this. I’ve seen another post discussing something like this and the solution includes that line of code, and the creator marked it as a solution.

I’m unsure. I tried this i not own code in a running script too. Lemme check rq.