I dont see what im doing wrong here. Can anyone help?
01 | local runanim = game.Players.LocalPlayer.Character:WaitForChild( "Humanoid" ):LoadAnimation(script.Running) |
02 |
03 | uis.InputBegan:Connect( function (key) |
04 | if key.KeyCode = = Enum.KeyCode.LeftShift then |
05 | plr.Character.Humanoid.WalkSpeed = 20 |
06 | runanim.Looped = true |
07 | runanim:Play() |
08 | end |
09 | end ) |
10 |
11 | uis.InputEnded:Connect( function (key) |
12 | if key.KeyCode = = Enum.KeyCode.LeftShift then |
13 | plr.Character.Humanoid.WalkSpeed = 16 |
14 | runanim.Looped = false |
15 | runanim:Stop() |
16 | end |
17 | 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.
01 | local player = game.Players.LocalPlayer |
02 | local char = player.Char or player.CharacterAdded:Wait() |
03 | local runanim = char:WaitForChild( "Humanoid" ):LoadAnimation(script.Running) |
04 |
05 | uis.InputBegan:Connect( function (key) |
06 | if key.KeyCode = = Enum.KeyCode.LeftShift then |
07 | char.Humanoid.WalkSpeed = 20 |
08 | runanim.Looped = true |
09 | runanim:Play() |
10 | end |
11 | end ) |
12 |
13 | uis.InputEnded:Connect( function (key) |
14 | if key.KeyCode = = Enum.KeyCode.LeftShift then |
15 | char.Humanoid.WalkSpeed = 16 |
16 | runanim.Looped = false |
17 | runanim:Stop() |
18 | end |
19 | 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.
01 | local player = game.Players.LocalPlayer |
02 | local char = player.Char or player.CharacterAdded:Wait() |
03 | local runanim = char:WaitForChild( "Humanoid" ):LoadAnimation(script.Running) |
04 |
05 | uis.InputBegan:Connect( function (key, gp) |
06 | if key.KeyCode = = Enum.KeyCode.LeftShift and not gp then |
07 | char.Humanoid.WalkSpeed = 20 |
08 | runanim.Looped = true |
09 | runanim:Play() |
10 | end |
11 | end ) |
12 |
13 | uis.InputEnded:Connect( function (key) |
14 | if key.KeyCode = = Enum.KeyCode.LeftShift then |
15 | char.Humanoid.WalkSpeed = 16 |
16 | runanim.Looped = false |
17 | runanim:Stop() |
18 | end |
19 | 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!
01 | UIS = game:GetService( "UserInputService" ) |
02 | local Character = script.Parent |
03 | local Humanoid = Character.Humanoid |
04 | local Anim = script:WaitForChild( "Running" ) |
05 | local Track = Humanoid:LoadAnimation(Anim) |
06 |
07 | UIS.InputBegan:Connect( function (key) |
08 | if key.KeyCode = = Enum.KeyCode.LeftShift then |
09 | print ( "pressed!" ) |
10 | Humanoid.WalkSpeed = 20 |
11 | Track.Looped = true |
12 | Track:Play() |
13 | end |
14 | end ) |
15 |
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:
01 | local runanim = game.Players.LocalPlayer.Character:WaitForChild( "Humanoid" ):LoadAnimation(script.Running) |
02 | local plr = game.Players.LocalPlayer |
03 |
04 | uis.InputBegan:Connect( function (key) |
05 | if key.KeyCode = = Enum.KeyCode.LeftShift then |
06 | plr.Character.Humanoid.WalkSpeed = 20 |
07 | runanim.Looped = true |
08 | runanim:Play() |
09 | end |
10 | end ) |
11 |
12 | uis.InputEnded:Connect( function (key) |
13 | if key.KeyCode = = Enum.KeyCode.LeftShift then |
14 | plr.Character.Humanoid.WalkSpeed = 16 |
15 | runanim.Looped = false |
16 | runanim:Stop() |
17 | end |
18 | end ) |
and that would get the plr for you and the Character.