Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Attempt to Index field 'Character' a nil value?

Asked by 5 years ago

I dont see what im doing wrong here. Can anyone help?

01local runanim = game.Players.LocalPlayer.Character:WaitForChild("Humanoid"):LoadAnimation(script.Running)
02 
03uis.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
09end)
10 
11uis.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
17end)

3 answers

Log in to vote
1
Answered by
compUcomp 417 Moderation Voter
5 years ago

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.

01local player = game.Players.LocalPlayer
02local char = player.Char or player.CharacterAdded:Wait()
03local runanim = char:WaitForChild("Humanoid"):LoadAnimation(script.Running)
04 
05uis.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
11end)
12 
13uis.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
19end)

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.

01local player = game.Players.LocalPlayer
02local char = player.Char or player.CharacterAdded:Wait()
03local runanim = char:WaitForChild("Humanoid"):LoadAnimation(script.Running)
04 
05uis.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
11end)
12 
13uis.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
19end)
Ad
Log in to vote
0
Answered by
bum5Br 97
5 years ago
Edited 5 years ago

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!

01UIS = game:GetService("UserInputService")
02local Character = script.Parent
03local Humanoid = Character.Humanoid
04local Anim = script:WaitForChild("Running")
05local Track  = Humanoid:LoadAnimation(Anim)
06 
07UIS.InputBegan:Connect(function(key)
08    if key.KeyCode == Enum.KeyCode.LeftShift then
09print("pressed!")
10        Humanoid.WalkSpeed = 20
11        Track.Looped = true
12        Track:Play()
13    end
14end)
15 
View all 23 lines...
Log in to vote
0
Answered by 5 years ago

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:

01local runanim = game.Players.LocalPlayer.Character:WaitForChild("Humanoid"):LoadAnimation(script.Running)
02local 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.

Answer this question