Index When Waiting For Humanoid

I am making a script but need Animator and for some reason it can’t find it, here is the error and output:
image

	local player = game.Players.LocalPlayer
	local character = player.Character
	local animator = character.Humanoid:WaitForChild("Animator")
	local shotEvent = replicatedStorage.Remotes:WaitForChild("shotEvent")

line 12 is the animator line.

I think it’s because the character is not fully loaded
try this

    local player = game.Players.LocalPlayer
	local character = player.Character or player.CharacterAdded:Wait()
	local animator = character.Humanoid:WaitForChild("Animator")
	local shotEvent = replicatedStorage.Remotes:WaitForChild("shotEvent")
1 Like

You have to wait for the character or the Character will return nil cuz it wouldn’t exist if you index it that early:

local character = player.Character or player.CharacterAdded:Wait()

(woops late reply)

Yeah I tried that but it is saying humanoid is not a valid member of the palyer

Yeah I tried that but it is saying humanoid is not a valid member of the palyer

If it is a weapon in the backpack you can try:

script.Parent.Parent.Parent.Humanoid.Animator

That might work.

Its having a problem finding a humanoid
image

Maybe this will help you out!

Is it a local script or a normal one?

try using WaitForChild(“Humanoid”)

Figured it out theres a glitch wtih roblox just gotta use

repeat wait() until player.Character

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local animator = character.Humanoid:FindFirstChildOfClass("Animator")
local shotEvent = replicatedStorage.Remotes:WaitForChild("shotEvent")

That should work! :slightly_smiling_face:

Of course, you can still use Humanoid:LoadAnimation(), however, that’s a last resort if Animator still can’t be found. It may also be a backend problem with the code you’re using, so keep that in mind aswell!

Figured it out theres a glitch wtih roblox just gotta use this instead since its a glitch my friend helped me

repeat wait() until player.Character

Using a loop for the character is actually not a solution, but instead more work that isn’t needed. In addition, I recommend you use task.wait() instead of wait(), due to it being a more standardized wait nowadays.

Also, please mark someone solution to help the SEO. :slightly_smiling_face: