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

attempt to index nil with 'Humanoid' ?

Asked by 3 years ago

hello guys, I have a problem with my walk / run script. I wanted to add an animation that will play when the player starts to run. But I keep getting an error.

local UserInput = game:GetService("UserInputService")
local Player = game.Players.LocalPlayer
local Character = Player.Character
local sprintspeed = 24
local walkspeed = 16
local sprinting = false 

local function BeginSprint(input,gameProcessed)
    if not gameProcessed then 
        if input.UserInputType == Enum.UserInputType.Keyboard then

            local keycode = input.KeyCode
            if  keycode  == Enum.KeyCode.LeftControl then
                sprinting = not sprinting
                if sprinting then 
                    Player.Character.Humanoid.WalkSpeed = sprintspeed
                    local Anim = Instance.new('Animation')
                    Anim.AnimationId = 'rbxassetid://7217974168'
                    local PlayAnim = Character.Humanoid:LoadAnimation(Anim)
                    PlayAnim:Play()
                else
                    Player.Character.Humanoid.WalkSpeed = walkspeed
                end

            end
        end
    end
end


UserInput.InputBegan:connect(BeginSprint)

error:

Players.OleshaDumayet.PlayerScripts.RunningScript:22: attempt to index nil with 'Humanoid'  

I thought maybe I should create a variable for Humanoid like this:

local Humanoid = Player.Character:WaitForChild('Humanoid')

but no its still giving me an error:

 Players.OleshaDumayet.PlayerScripts.RunningScript:4: attempt to index nil with 'WaitForChild'

1 answer

Log in to vote
0
Answered by
TGazza 1336 Moderation Voter
3 years ago

Assuming this is a LocalScript. Change this line:

local Player = game.Players.LocalPlayer
local Character = Player.Character -- << here

To

local Player = game.Players.LocalPlayer
local Character = Player.CharacterAdded:Wait() -- <<  this will wait for the character to load before continuing!
0
yeah you are right, error does not appear anymore, but the problem is that the animation still does not start :( OleshaDumayet 9 — 3y
Ad

Answer this question