Error: Attempt to index nil with 'Humanoid'

I’ve been doing some testing with programming animations. Basically, I have created an animation and when a player joins I want it to loop forever.

Here is my script:

local anim = script.Animation

wait(1)

local player = game.Players.LocalPlayer
local char = player.Character
local hum = char.Humanoid --This is where the error is

local animDribbleRTrack = hum:LoadAnimation(anim)

while true do
	wait(5)
	animDribbleRTrack:Play()
end

image

I’ve tried to search the forum for a solution with no success.

4 Likes

I re-wrote your code and I believe it should work better! Let me know if it is still bugging around:

local Animation = script.Animation

task.wait(1)

local Players = game:GetService(“Players”)
local Player = Players.LocalPlayer
local Character = player.Character
local Humanoid = Character:FindFirstChildOfClass(“Humanoid”) 

local animDribbleRTrack = Humanoid:LoadAnimation(Animation)

while true do
	task.wait(5)
	animDribbleRTrack:Play()
end
1 Like

Thanks for replying!
For some reason, it gives me the following error: “attempt to index nil with FindFirstChildOfClass”

1 Like

You could add a wait for child for the humanoid

local hum = char:WaitForChild(“Humanoid”)
1 Like

try using the CharacterAdded event, like so:

local player = game.Players.LocalPlayer
local function onCharacterAdded()
 --load and play animation
end
player.CharacterAdded:Connect(onCharacterAdded)

Update: I figured out that my character and my humanoid is actually found in the workspace when I join the game… So this is how I modified my code:

local anim = script.Animation

wait(1)

local player = game.Players.LocalPlayer
local char = game.Workspace.DMLGDPCC
local hum = char.Humanoid

local animDribbleRTrack = hum:LoadAnimation(anim)

while true do
	wait(5)
	animDribbleRTrack:Play()
end

V Workspace
image

image

With this, I successfully had my animation working! So here is the problem: how do I modify this code for it to work for every player in the game?

-- Get player
local player = game:GetService("Players").LocalPlayer

-- Wait for animation
local animation = script:WaitForChild("Animation")

-- Make this variable local
local animDribbleRTrack = nil

-- Make a function that takes a character and loads an animation on it
-- Then sets the animDribbleRTrack variable to be that animation
local function onCharacterAdded(character)
	local humanoid = character:WaitForChild("Humanoid")
	local animator = humanoid:WaitForChild("Animator")

	animDribbleRTrack = animator:LoadAnimation(animation)
end

-- If there is already a character, then load the animation on it
if player.Character then
	onCharacterAdded(player.Character)
end
-- When the player gets a new character, load the animation on it
player.CharacterAdded:Connect(onCharacterAdded)

-- Every 5 seconds, if there is an animation, play it
while true do
	task.wait(5)
	if animDribbleRTrack then
		animDribbleRTrack:Play()
	end
end

you put the localscript under StarterPlayerScripts

this runs as soon as the player loads in
this does not run when the character loads in which is a little bit after

move it into StarterCharacterScripts
no changes to the script are required

1 Like

you should not do this since that would only work for your player not other players

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.