Run animation resets when any key is pressed

I’m trying to make a running script, which will make player run on certain conditions (When player press Shift key + W/S) But the animation keep resets itself when i press any button.
And when I let go of W/S and still holding Shift, the script thinks I’m still running.

This is the whole thing:
image_2022-09-19_235707616

This below is my “Run” script:

-- --Garbage script

local RunSpeed = 40-- ///Set running speed
--//////////////////////////-- 
local Player = game.Players.LocalPlayer
local Stamina = script.Parent:WaitForChild("CurrentStamina")
local IsSprinting = script.Parent:WaitForChild("IsSprinting")
local Character = workspace:WaitForChild(Player.Name)
local Humanoid = Character:WaitForChild('Humanoid')
local RunAnimation = Instance.new('Animation')
RunAnimation.AnimationId = 'rbxassetid://10891361825'---////Run Animation ID

RAnimation = Humanoid:LoadAnimation(RunAnimation)

local SoundFX = script.RunSound
--//////////////////////////

local W, S = Enum.KeyCode.W, Enum.KeyCode.S


local Key = Enum.KeyCode.LeftShift 
local Camera = workspace.CurrentCamera
local UserInputService = game:GetService("UserInputService")


local function WPressed()
	return UserInputService:IsKeyDown(W)
end

local function SPressed()
	return UserInputService:IsKeyDown(S)
end


local function ShiftPressed()
	return UserInputService:IsKeyDown(Key)
end

local function Input(input, gameProcessedEvent)
	if ShiftPressed()	then
        print("Player is running")
		 if WPressed() then
				RAnimation:Play()
			SoundFX:Play()
			IsSprinting.Value = true
				Player.Character.Humanoid.WalkSpeed = RunSpeed
		else if SPressed() then 
			
				SoundFX:Stop()
				IsSprinting.Value = true
				end
			end
	else
		print("Player will not run")
		IsSprinting.Value = false
		end
	end


local function InputEnded(input)
	if input.KeyCode == Key then
		IsSprinting.Value = false
		RAnimation:Stop()
	
		SoundFX:Stop()
		print("Player stopped running")
		Player.Character.Humanoid.WalkSpeed = 20
	end
end

UserInputService.InputBegan:Connect(Input)	
UserInputService.InputEnded:Connect(InputEnded)
```
1 Like

The walk animation is resetting because you’re not checking if it’s already playing. To do that this:
image
will do.

2 Likes

Also for sprinting if you just want to check if they’re moving (also make sure your sprinting bool is checked to true) Humanoid.Running works, just check if they’re sprinting in there to play your running animation.

1 Like

If the game has mobile players I’d recommend using ContextActionService. It allows you to create a mobile button if you don’t wanna make your own.

1 Like

Thank you, but my problem here is that when I press any button while holding Shift (Run), the run animation play and bugged itself, even though i coded that player must hold Shift and W/S in order to run.
This is what it looks like: (Sorry for low quality footage)


Maybe your solutions were right, and it’s just that i didnt know where to put your code in ?

It goes in where ever you’d like the run animation to be played.
https://developer.roblox.com/en-us/api-reference/property/AnimationTrack/IsPlaying

2 Likes

Please, please for the love of god use contextactionservice so it doesn’t trigger when you’re typing.

2 Likes

My replies were so terribly explained. But if you want it to extend beyond keyboard inputs I’d use Humanoid.Running just to make it as simple as possible. It fires whenever the speed changes slightly though.