Running animation isn't working

I’m currently facing an issue with a script I’ve been working on. The script is supposed to increase the character’s walk speed and play an animation when the shift key is pressed. However, nothing seems to happen when I press shift. I’ve included the script below:

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

UIS.InputBegan:connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		Character.WaitForChild("Humanoid").WalkSpeed = 35
		local Anim = Instance.new('Animation')
		Anim.AnimationId = 'rbxassetid:/18135153349'
		PlayAnim = Character.Humanoid:LoadAnimation(Anim)
		PlayAnim:Play()
	end
end)

UIS.InputEnded:connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		Character.WaitForChild("Humanoid").WalkSpeed = 16
		PlayAnim:Stop()
	end
end)


This script is located in a LocalScript within StarterCharacterScripts. I’m not receiving any errors, but the expected behavior is not occurring. I would greatly appreciate any insights or suggestions on what might be causing this issue.

local Character = Player:WaitForChild(“Character”) won’t do anything, and just leaves it to infinitely search for a child of Player named Character. You need to do local Character = Player.Character

Character.WaitForChild is meant to be Character:WaitForChild since you’re attempting to call a function. This issue occurs at lines 7 and 17.

There are no more issues other than the two I’ve mentioned.

Fixed code
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:WaitForChild("Humanoid").WalkSpeed = 35
		local Anim = Instance.new('Animation')
		Anim.AnimationId = 'rbxassetid:/18135153349'
		PlayAnim = Character.Humanoid:LoadAnimation(Anim)
		PlayAnim:Play()
	end
end)

UIS.InputEnded:connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		Character:WaitForChild("Humanoid").WalkSpeed = 16
		PlayAnim:Stop()
	end
end)

14:16:47.475 Players.JellyM015.PlayerScripts.goofy ahh run:7: attempt to index nil with 'WaitForChild' - Client - goofy ahh run:7

I have no idea what is causing the script to break when it’s placed in StarterPlayerScripts? It works fine when it’s inside StarterCharacterScripts though.

1 Like

I put it in StarterCharacterScripts and I get even more errors. The sprint works, but the animation doesn’t.

  14:20:42.809  Workspace.JellyM015.goofy ahh run:18: attempt to index nil with 'Stop'  -  Client - goofy ahh run:18
  14:20:43.023  Invalid animation id '<error: unknown AssetId protocol>':   -  Client - goofy ahh run:10

the animation id is rbxassetid:/18135153349 instead of rbxassetid://18135153349

You should load the Animation outside of the function. You should do it like this, I believe this works.
EDIT: Just noticed I omitted the AnimationId.

local UIS = game:GetService('UserInputService')
local Player = game.Players.LocalPlayer
local Character = Player.Character
local AnimationsFolder = Instance.new("Folder", Character)
local RunAnim = Instance.new('Animation')
RunAnim.AnimationId = "rbxassetid://18135153349"
RunAnim.Parent = AnimationsFolder
RunAnim.Name = "RunAnimation"
Character:WaitForChild("Humanoid"):LoadAnimation(RunAnim)

UIS.InputBegan:connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		Character:WaitForChild("Humanoid").WalkSpeed = 35
		RunAnim:Play()
	end
end)

UIS.InputEnded:connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		Character:WaitForChild("Humanoid").WalkSpeed = 16
		RunAnim:Stop()
	end
end)
1 Like
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:WaitForChild("Humanoid").WalkSpeed = 35
		local Anim = Instance.new('Animation')
		Anim.AnimationId = "rbxassetid://18135153349"
		PlayAnim = Character.Humanoid:LoadAnimation(Anim)
		PlayAnim:Play()
	end
end)

UIS.InputEnded:connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		Character:WaitForChild("Humanoid").WalkSpeed = 16
		PlayAnim:Stop()
	end
end)

place this in startercharacterscripts, when i tested it worked fine for me
wrong reply mb

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.