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

Why is the error "attempt to index a nil value" showing up?

Asked by 6 years ago

I have a LocalScript inside a tool that is supposed to make it auto-equip. However, the same error (attempt to index a nil value) keeps appearing no matter how I change the script.

Script:

while true do
    wait(.5)
game.Workspace:FindFirstChild(game.Players.LocalPlayer).Humanoid:EquipTool(script.Parent)
end

1 answer

Log in to vote
0
Answered by 6 years ago

The script will immediately find the LocalPlayer, which is successful, but not the Humanoid, which is not a property of LocalPlayer. Access the Character from the LocalPlayer first before the Humanoid. Also, to make it work, your code must wait until the local player's character loaded. This is the correct way:

localplr = game.Players.LocalPlayer
char = localplr.CharacterAdded:wait() -- Use the event CharacterAdded, then add ":wait()"
while true do
    wait(.5)
    if char:FindFirstChild('Humanoid') then
        -- your code
    end
end

You're welcome.

Ad

Answer this question