Search code examples
luaroblox

My script is not working and its giving me this specific error "Players.73737s2.PlayerScripts.LocalScript:4: attempt to index nil with 'Humanoid'"


local player = game.Players.LocalPlayer

local walk = 0
player.Character.Humanoid.WalkSpeed = 1
while true do
wait(1)
player.Character.Humanoid.WalkSpeed = walk
walk = walk + 1
end

for some unknown reason it gives me error Players.73737s2.PlayerScripts.LocalScript:4: attempt to index nil with 'Humanoid'


Solution

  • Characters can take some time to load depending on a few factors. During this time the character will appear nil until the character is fully loaded. You can do this two ways either with a function or a repeat wait.

    Function

    local Player = game.Players.LocalPlayer -- Script is hopefully in a Client Side area.
    
    Player.CharacterAdded:Connect(function(Character)
    -- Your code here.
    end)
    

    Waiting

    local Player = game.Players.LocalPlayer -- Script is hopefully in a Client Side area.
    
    repeat wait() until Player.Character
    -- Your Code
    

    I hope this helps and I apologize for the lack of formatting in my answer as I am still learning StackOverflow, If I said anything wrong I will attempt to correct my issue.