Attempt to index nil with 'Humanoid'

My code is supposed to make an animation play for an idle animation. Its a rocket launcher, but the problem with it is that… It for some reason isn’t finding Humanoid, even though it has many times before?

I’ve tried doing :FindFirstChild and :WaitForChild, or character.Humanoid, but still nothing.

The animation priority is set to Idle, the animation is inside the localscript, which is inside the tool.

I will try to show what everything does, heres my code:

local tool = script.Parent -- gets the tool

local player = game.Players.LocalPlayer -- gets the player

local character = player.Character -- gets the character from the player


--idle
local idle = script:WaitForChild("Idle") -- idle is inside the script
local IdleAnim = character.Humanoid:LoadAnimation(idle) -- loads the animation, reference to the things i've already tried

tool.Equipped:Connect(function() -- if its equipped, play the animation
	IdleAnim:Play()
end)

tool.Unequipped:Connect(function() -- if unequipped, stop the animation.
	IdleAnim:Stop()
end)

Anything helps.

Sorry for the very late reply, but perhaps the problem is that the script is loading too fast, and the character has not yet been added in yet, so you can do

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

I think I might know your problem.
Humanoid:LoadAnimation() is deprecated.
Use Humanoid.Animator:LoadAnimation() instead.

1 Like

It just said that the character is nil…

attempt to index nil with 'Character'

Is this a local script? If so, then you can’t use game.Players.LocalPlayer.

try this:

local tool = script.Parent -- gets the tool

local player = game.Players.LocalPlayer -- gets the player

repeat wait() until player.Character
local character = player.Character -- gets the character from the player


--idle
local idle = script:WaitForChild("Idle") -- idle is inside the script
local IdleAnim = character.Humanoid:LoadAnimation(idle) -- loads the animation, reference to the things i've already tried

tool.Equipped:Connect(function() -- if its equipped, play the animation
	IdleAnim:Play()
end)

tool.Unequipped:Connect(function() -- if unequipped, stop the animation.
	IdleAnim:Stop()
end)
local players = game:GetService("Players")
local player = players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")

local tool = script.Parent

local animation = script:WaitForChild("Idle")
local track = humanoid:LoadAnimation(animation)

tool.Equipped:Connect(function()
	track:Play()
end)

tool.Unequipped:Connect(function()
	track:Stop()
end)

Its a modulescript, built like a localscript, so basically. yea

Still says that character is nil?

Yep again, still character is nil… Can module scripts be used as a local script technically? Or are they just serverscripts. Either way, still not sure why character is nil. That might be my problem

ModuleScripts follow the context in which they were required in, they are treated as local if required by a local script script & treated as server if required by a server script.

Oh… I’m calling it by server script. should i change it to local script?

Yes, that would be necessary.

game.Players.LocalPlayer is not defined in server scripts.

Alright: Update, tried it and it gave me this error:
Equipped is not a valid member of Folder “Workspace.squaredsnow.RocketLauncher.Modules”

I just copy and pasted the script into a local script because theres nothing that would change. And the localscript is in the same place the serverscript was…

The script should be placed inside the tool.

It is inside the tool, ill try to send a screenshot.

Capture

I mean directly inside, currently script.Parent is a reference to the “Modules” folder, as the error message states.

1 Like

I changed the function name to Equip instead of Equipped, still the same error, i made a variable for the tool, and checked if it was equipped, but same error, heres where the error is:

local tool = script.Parent

	local animation = script:WaitForChild("Idle")
	local track = humanoid:LoadAnimation(animation)

	tool.Equipped:Connect(function() -- error Equipped is not a valid member of Folder "Workspace.squaredsnow.RocketLauncher.Modules"
		print("Track played")
		track:Play()
	end)

OH WAIT, I see where i went wrong. One second.