How do I make it so a run animation plays if a person is holding shift and running?

I have a issue with a script where if you hold shift at all it plays the run animation even if you aren’t running. It is really bugging me and a friend right now and we wanna fix it. Is there a way that I can see if someone is running and if they are the animation plays and their walk speed increases? Also another thing I wanna point out is if you jump while running the animation stops too and the normal run starts. (I am more of a builder than scripter btw)

Here’s my script:
local UIS = game:GetService(‘UserInputService’)
local Player = game.Players.LocalPlayer
local Character = Player.Character

UIS.InputBegan:connect(function(input)
if input.KeyCode == Enum.KeyCode.LeftShift then
Character.Humanoid.WalkSpeed = 24
local Anim = Instance.new(‘Animation’)
Anim.AnimationId = ‘rbxassetid://5837250013’
PlayAnim = Character.Humanoid:LoadAnimation(Anim)
PlayAnim:Play()
for i = 1,5 do
game.Workspace.CurrentCamera.FieldOfView = (70+(i*2))
wait()
end
end
end)

UIS.InputEnded:connect(function(input)
if input.KeyCode == Enum.KeyCode.LeftShift then
Character.Humanoid.WalkSpeed = 16
PlayAnim:Stop()
for i = 1,5 do
game.Workspace.CurrentCamera.FieldOfView = (80-(i*2))
wait()
end
end
end)

You didn’t address PlayAnim, please put your code between 3 Ticks ( ``` ), and, format your code if possible. It makes it easier for at least, me, to understand.

try using :GetMoveVector() from the control module.

in your InputBegans if statement, i added this

controls:GetMoveVector() ~= Vector3.new(0,0,0)

local playerModule = require(player.PlayerScripts:WaitForChild("PlayerModule"))
local controls = playerModule:GetControls()

local UIS = game:GetService("UserInputService")
local Player = game.Players.LocalPlayer
local Character = Player.Character
local PlayAnim

UIS.InputBegan:connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift and controls:GetMoveVector()~=Vector3.new(0,0,0) then
		Character.Humanoid.WalkSpeed = 24
		local Anim = Instance.new("Animation")
		Anim.AnimationId = "rbxassetid://5837250013"
		PlayAnim = Character.Humanoid:LoadAnimation(Anim)
		PlayAnim:Play()
		for i = 1,5 do
			game.Workspace.CurrentCamera.FieldOfView = (70+(i*2))
			wait()
		end
	end
end)

UIS.InputEnded:connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		Character.Humanoid.WalkSpeed = 16
		if(PlayAnim)then
		   PlayAnim:Stop()
		end
		for i = 1,5 do
			game.Workspace.CurrentCamera.FieldOfView = (80-(i*2))
			wait()
		end
	end
end)

Hi, I noticed a few things about your version of the code that I would like to point out:

  1. You are using :connect() instead of the new (it’s not even new anymore) :Connect(). (I know that’s how the code came, but you can always fix the little things)

  2. When playing anims with key inputs, you always want to have some sort of check to make sure the player isn’t typing because it could activate the animation at the wrong time if not implemented.

Here is the re-finalized version:

local playerModule = require(player.PlayerScripts:WaitForChild("PlayerModule"))
local controls = playerModule:GetControls()

local UIS = game:GetService("UserInputService")
local Player = game.Players.LocalPlayer
local Character = Player.Character
local PlayAnim

UIS.InputBegan:Connect(function(input, gameProcessed)
	if not gameProcessed then
		if input.KeyCode == Enum.KeyCode.LeftShift and controls:GetMoveVector()~=Vector3.new(0,0,0) then
			Character.Humanoid.WalkSpeed = 24
			local Anim = Instance.new("Animation")
			Anim.AnimationId = "rbxassetid://5837250013"
			PlayAnim = Character.Humanoid:LoadAnimation(Anim)
			PlayAnim:Play()
			for i = 1,5 do
				workspace.CurrentCamera.FieldOfView = (70+(i*2))
				wait()
			end
		end
	end
end)

UIS.InputEnded:Connect(function(input, gameProcessed)
	if not gameProcessed then
		if input.KeyCode == Enum.KeyCode.LeftShift then
			Character.Humanoid.WalkSpeed = 16
			if(PlayAnim)then
		   		PlayAnim:Stop()
			end
			for i = 1,5 do
				workspace.CurrentCamera.FieldOfView = (80-(i*2))
				wait()
			end
		end
	end
end)

you know gameProcessedEvent isn’t just for typing and he wasn’t asking for help on :connect information or how InputBegan is actually used, but yes your right.

Updated Code
local playerModule = require(player.PlayerScripts:WaitForChild("PlayerModule"))
local controls = playerModule:GetControls()

local UIS = game:GetService("UserInputService")
local Player = game.Players.LocalPlayer
local Character = Player.Character or player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local PlayAnim

UIS.InputBegan:Connect(function(input, g)
	if g then return end

	if input.KeyCode == Enum.KeyCode.LeftShift and controls:GetMoveVector()~=Vector3.new(0,0,0) then
		Humanoid.WalkSpeed = 24
		local Anim = Instance.new("Animation")
		Anim.AnimationId = "rbxassetid://5837250013"
		PlayAnim = Humanoid:LoadAnimation(Anim)
		PlayAnim:Play()
		for i = 1,5 do
			game.Workspace.CurrentCamera.FieldOfView = (70+(i*2))
			wait()
		end
	end
end)

UIS.InputEnded:Connect(function(input, g)
	if g then return end

	if input.KeyCode == Enum.KeyCode.LeftShift then
		Humanoid.WalkSpeed = 16
		if(PlayAnim)then
		   PlayAnim:Stop()
		end
		for i = 1,5 do
			game.Workspace.CurrentCamera.FieldOfView = (80-(i*2))
			wait()
		end
	end
end)

but since we’re going that far, lets go ahead and make sure the character and humanoid exist before we even allow them to use the input.

local Player = game.Players.LocalPlayer
local Character = Player.Character or player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")

Does all this code just go in the same script? I have my code in a local script in StarterCharacterScripts. I am a little confused about everything you guys are saying cause Idk much about roblox scripting.

yes just replace your whole script with the one i updated above, if there is any typos then im sorry. :stuck_out_tongue:

I tested it on studio and on Roblox and it didn’t work.

any errors? and did you grab the updated code?

21:08:47.822 Workspace.Zvtu.RunningScript:1: attempt to index nil with ‘PlayerScripts’ - Client - RunningScript:1
&
Requested module experienced an error while loading - Studio

I also did update the code but it just doesn’t work.

my fault, set up your variables like this instead

local UIS = game:GetService("UserInputService")
local Player = game.Players.LocalPlayer
local playerModule = player:WaitForChild("PlayerScripts"):WaitForChild("PlayerModule")
local controls = require(playerModule:WaitForChild("ControlModule"))
local Character = Player.Character
local PlayAnim

the player wasn’t referenced yet when it tried to get the Player/Control module.

Player speed change and player animation

This code when placed inside a LocalScript in StarterPlayer in StarterCharacterScripts

local UIS = game:GetService("UserInputService")
local humanoid = script.Parent:WaitForChild("Humanoid")
local Enable = false

local Ninja = Instance.new("Animation")
Ninja.Name = "Ninja"
Ninja.AnimationId = "http://www.roblox.com/asset/?id=656118852"
Ninja.Parent = script

humanoid.AnimationPlayed:Connect(function(animationTrack)
if animationTrack.Name == "RunAnim" and Enable == true then
Ninja = humanoid:LoadAnimation(script.Ninja)
Ninja:Play()
end
end)

UIS.InputBegan:connect(function(input)
if input.KeyCode == Enum.KeyCode.LeftShift then
Enable = true
humanoid.WalkSpeed = 24
Ninja = humanoid:LoadAnimation(script.Ninja)
Ninja:Play()
for i = 1,5 do
game.Workspace.CurrentCamera.FieldOfView = (70+(i*2))
wait()
end
end
end)

UIS.InputEnded:connect(function(input)
if input.KeyCode == Enum.KeyCode.LeftShift then
Enable = false
humanoid.WalkSpeed = 16
Ninja:Stop()
for i = 1,5 do
game.Workspace.CurrentCamera.FieldOfView = (80-(i*2))
wait()
end
end
end)
1 Like

Does this not work with custom run animations?

Also, I wanna make it so after you jump, the animation will continue and not break.

Hey, this works but the it won’t play my custom animation. Also, how can I make it so if the player jumps it won’t stop the animation after they jump?

Just do this

Ninja.AnimationId = “Add here the animation you want”

And don’t change anything

If it doesn’t work, tell me about the problem

21:44:44.482 Animation “https://assetdelivery.roblox.com/v1/asset?id=5837250013&serverplaceid=0” failed to load in “Workspace.Zvtu.RunningScript.Ninja.AnimationId”: Animation failed to load - Client - AnimationId

21:44:44.485 Animation “https://assetdelivery.roblox.com/v1/asset?id=5837250013&serverplaceid=0” failed to load in “Animation.AnimationId”: Animation failed to load

The error is in the animation link and not in the code you sent

Put the code you sent in the studio without changing anything

If it doesn’t work properly tell me about the problem

Try an animation that works properly

Check the link you copied