I dont see what im doing wrong here. Can anyone help?
local runanim = game.Players.LocalPlayer.Character:WaitForChild("Humanoid"):LoadAnimation(script.Running) uis.InputBegan:Connect(function(key) if key.KeyCode == Enum.KeyCode.LeftShift then plr.Character.Humanoid.WalkSpeed = 20 runanim.Looped = true runanim:Play() end end) uis.InputEnded:Connect(function(key) if key.KeyCode == Enum.KeyCode.LeftShift then plr.Character.Humanoid.WalkSpeed = 16 runanim.Looped = false runanim:Stop() end end)
The problem here is that the character hasn't loaded yet. When you get the character, check if the character exists, and if it doesn't you need to wait for CharacterAdded.
local player = game.Players.LocalPlayer local char = player.Char or player.CharacterAdded:Wait() local runanim = char:WaitForChild("Humanoid"):LoadAnimation(script.Running) uis.InputBegan:Connect(function(key) if key.KeyCode == Enum.KeyCode.LeftShift then char.Humanoid.WalkSpeed = 20 runanim.Looped = true runanim:Play() end end) uis.InputEnded:Connect(function(key) if key.KeyCode == Enum.KeyCode.LeftShift then char.Humanoid.WalkSpeed = 16 runanim.Looped = false runanim:Stop() end end)
Not related to your question, but you should also make sure that when the shift key is held to make a capital letter in the chat, it doesn't trigger the running animation.
local player = game.Players.LocalPlayer local char = player.Char or player.CharacterAdded:Wait() local runanim = char:WaitForChild("Humanoid"):LoadAnimation(script.Running) uis.InputBegan:Connect(function(key, gp) if key.KeyCode == Enum.KeyCode.LeftShift and not gp then char.Humanoid.WalkSpeed = 20 runanim.Looped = true runanim:Play() end end) uis.InputEnded:Connect(function(key) if key.KeyCode == Enum.KeyCode.LeftShift then char.Humanoid.WalkSpeed = 16 runanim.Looped = false runanim:Stop() end end)
From what i'm seeing here you don't have a player variable to call "plr", and that's why it returns the index as nil. If you put the script inside of the StarterCharacterScripts folder, ( which is inside of StarterPlayer ), you can get the Character only by using script.Parent, since the script will be parented to it as soon as the character is loaded, which is what i did here on this script. I renamed and separated some variables just to make it easier to understand. Hope this helps!
UIS = game:GetService("UserInputService") local Character = script.Parent local Humanoid = Character.Humanoid local Anim = script:WaitForChild("Running") local Track = Humanoid:LoadAnimation(Anim) UIS.InputBegan:Connect(function(key) if key.KeyCode == Enum.KeyCode.LeftShift then print("pressed!") Humanoid.WalkSpeed = 20 Track.Looped = true Track:Play() end end) UIS.InputEnded:Connect(function(key) if key.KeyCode == Enum.KeyCode.LeftShift then print("unpressed!") Humanoid.WalkSpeed = 16 Track.Looped = false Track:Stop() end end)
so basically you dont have a plr variable which you can get character from. or thats not your entire script.
im guessing your using a local script so this is what you would have to do to get the plr:
local runanim = game.Players.LocalPlayer.Character:WaitForChild("Humanoid"):LoadAnimation(script.Running) local plr = game.Players.LocalPlayer uis.InputBegan:Connect(function(key) if key.KeyCode == Enum.KeyCode.LeftShift then plr.Character.Humanoid.WalkSpeed = 20 runanim.Looped = true runanim:Play() end end) uis.InputEnded:Connect(function(key) if key.KeyCode == Enum.KeyCode.LeftShift then plr.Character.Humanoid.WalkSpeed = 16 runanim.Looped = false runanim:Stop() end end)
and that would get the plr for you and the Character.