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)
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.
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)
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