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

Why does it say Attempt to index nil with Humanoid?

Asked by 3 years ago

It ran an error and this is my code:

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local Hum = player.Character.Humanoid
local animation = script.Animation
local kick = Hum:LoadAnimation(animation)

mouse.Button1Down:Connect(function()
    kick:Play()
end)

The script is a LocalScript in

StarterPlayer > StarterPlayerScripts > LocalScript

2 answers

Log in to vote
2
Answered by 3 years ago
Edited 3 years ago

I would recommend putting the script in StarterCharacterScripts since it modifies the character. Since the player loads in before the character when someone joins, it can cause issues like this where the character doesn't exist yet but it still tries to run the rest of the code.

Otherwise, you could probably put player.CharacterAdded:Wait() under the first line of your code to make sure the character exists before trying to do anything else if you don't want to move it

0
It is redundant to put scripts inside StarterCharacterScripts on this situation, just do the second one Xapelize 2658 — 3y
Ad
Log in to vote
0
Answered by 3 years ago

put a player.Character:WaitForChild("Humanoid") instead of player.Character.Humanoid

0
I did that poppyloverb 20 — 3y

Answer this question