How to stop run animation after Key bind released

Hi everyone,

I’m struggling with making the running animation work properly. See, it does work when you press Right Shift but the problem is that it doesn’t stop when I release the key. How do I fix this please?

local Players = game:GetService("Players")
local anim = "rbxassetid://9724554081"

local runs = 25
local walks = 15
local P = Players.LocalPlayer
local a = Instance.new("Animation")
a.AnimationId = anim

local function beginScript(input, gameProcessed)
	if not gameProcessed then
		if input.UserInputType == Enum.UserInputType.Keyboard then
			local keycode = input.KeyCode
			if keycode == Enum.KeyCode.RightShift then
				P.Character.Humanoid.WalkSpeed = runs
				local lanim = P.Character.Humanoid:LoadAnimation(a)
				lanim:Play()
			end
		end
	end
end

local function endScript(input, gameProcessed)
	if not gameProcessed then
		if input.UserInputType == Enum.UserInputType.Keyboard then
			local keycode = input.KeyCode
			if keycode == Enum.KeyCode.RightShift then
				P.Character.Humanoid.WalkSpeed = walks
				local lanim = P.Character.Humanoid:LoadAnimation(a)
				lanim:Stop()
			end
		end
	end
end

userInput.InputBegan:Connect(beginScript)
userInput.InputEnded:Connect(endScript)

Thanks,
Aki

local UIS = game:GetService("UserInputService")
local Players = game:GetService("Players")
local RS = game:GetService("RunService")

local Player = Players.LocalPlayer
local Character = Player.CharacterAppearanceLoaded:Wait() and Player.Character
local Humanoid = Character:WaitForChild("Humanoid")
local Animator = Humanoid:WaitForChild("Animator")

local runs = 25
local walks = 15

local Animation = Instance.new("Animation")
Animation.AnimationId = "rbxassetid://9724554081"
local RunAnimation = Animator:LoadAnimation(Animation)
Animation:Destroy()

local KeyCode = Enum.KeyCode.LeftControl

local function Run(Key, gameProcessed)
	if gameProcessed then return end
	if Key.KeyCode == KeyCode then
		Humanoid.WalkSpeed = runs
		RunAnimation:Play()
		
		repeat RS.Heartbeat:Wait()
		until not UIS:IsKeyDown(KeyCode)
		
		Humanoid.WalkSpeed = walks
		RunAnimation:Stop()
	end
end

UIS.InputBegan:Connect(Run)
1 Like

hello, try putting local lanim = P.Character.Humanoid:LoadAnimation(a) before the whole script, instead using it twice inside functions.

1 Like

It didn’t seem to work? Hm. I also tried playing around with it but it still didn’t do anything…

press and hold LeftControl to run also it might be because your animation isnt looped

1 Like

It just gave an error “attempt to index with nil”

I did, the speed didn’t change nor the animation. I’ll try again for good measures. Edit: still no use

where did you parent the script

1 Like

The script is stored in StarterPlayerScript

put in startergui and it will work fine

1 Like

Nup, still nothing. I even tried changing the keycode but still nothing.

after this line put

print(Character, Humanoid, Animator)

and tell me the output

1 Like


uhhhhhh, no output?

script
local UIS = game:GetService("UserInputService")
local Players = game:GetService("Players")
local RS = game:GetService("RunService")

local Player = Players.LocalPlayer
local Character = Player.CharacterAppearanceLoaded:Wait() and Player.Character
local Humanoid = Character:WaitForChild("Humanoid")
local Animator = Humanoid:WaitForChild("Animator")
print(Character, Humanoid, Animator)

local runs = 25
local walks = 15

local Animation = Instance.new("Animation")
Animation.AnimationId = "rbxassetid://9724554081"
local RunAnimation = Animator:LoadAnimation(Animation)
Animation:Destroy()

local KeyCode = Enum.KeyCode.RightShift
local function Run(Key, gameProcessed)
	if gameProcessed then return end
	if Key.KeyCode == KeyCode then
		Humanoid.WalkSpeed = runs
		RunAnimation:Play()

		repeat RS.Heartbeat:Wait()
		until not UIS:IsKeyDown(KeyCode)

		Humanoid.WalkSpeed = walks
		RunAnimation:Stop()
	end
end

UIS.InputBegan:Connect(Run)

i see CharacterApperanceLoaded:Wait() stops the script replace

local Character = Player.Character or Player.CharactedAdded:Wait()

i used CharacterApperanceLoaded:Wait() because it often throws error when you try to load animation to unloaded character

1 Like

does it work after you reset character?

1 Like

I disabled Reset so it doesn’t exactly matter.