Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Im having trouble playing a animation it has a error?

Error: " 09:22:22.639 - ServerScriptService.Script:5: attempt to index nil with 'LoadAnimation'"

local uip = game:GetService("UserInputService")
local char = script.Parent
local human = char:WaitForChild("Humanoid", 1)
local anim = char:WaitForChild("Lookatwrist", 1)	
local animtrack = human:LoadAnimation(anim)
 
uip.InputBegan:Connect(function(input)
    if input.KeyCode == Enum.KeyCode.E then
        animtrack:Play()
        print("played")
    end
end)

i am trying to make it play a animation when you press e but it wont work please help

2 answers

attempt to index nil with 'Something' is a common error, and it means you're trying to index a value that's nil.

local human = char:WaitForChild('Humanoid', 1)
-- it's been 1 second, and no instance named Humanoid is found. the variable "human" is now nil
local animtrack = human:LoadAnimation(anim)
-- now, you're trying to call :LoadAnimation() on a value that's nil, not a humanoid as you expect

I've noticed that you're using the timeout argument of WaitForChild, meaning the return value will be nil if nothing is returned in that many seconds. Assuming script.Parent is the character, removing the timeout arguments should be your solution. Otherwise, properly handle nil values.

Potential solution:

-- replace these 2 lines:
local human = char:WaitForChild("Humanoid", 1)
local anim = char:WaitForChild("Lookatwrist", 1)    

-- with this (notice the removal of the second arguments):
local human = char:WaitForChild("Humanoid")
local anim = char:WaitForChild("Lookatwrist")    

Let me know if this is any help.

2 comments

0

nope just makes a inf yield

0

If that's the case, then there is no child named Humanoid in char. Unfortunately, you're not providing enough information for a meaningful solution. Make sure char is a character as you expect, and that there's a humanoid in that character named Humanoid. Where is your script located?

You probably remember Optikk's answer, well here might be the right answer:

local uip = game:GetService("UserInputService")
local char = script.Parent
local human = char:FindFirstChildOfClass("Humanoid") -- Finds first child of class so no matter what the humanoids name is, it recognises it as a humanoid
local anim = char:WaitForChild("Lookatwrist")    
local animtrack = human:LoadAnimation(anim)

uip.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.E then
		animtrack:Play()
		print("played")
	end
end)

so the problem is, its your humanoid's name is not called "Humanoid" which provides an infinite yield (which means its waiting forever for the humanoid to be present)

Hope this helps!

This discussion is preserved as it was. New answers, comments, and votes are closed.