Attempt to index nil with Humanoid when loading animation

Hi, I’ve been having trouble with this bit of code I wrote trying to do a sword combo. I’ve looked other places but I must have been bad with wording while searching because I wasn’t figuring out an answer. I can’t figure out why exactly it is erroring when I try to load the animation, as my other animation scripts have the code to load animations written in the same way and they work fine. Thank you for taking the time to read and help me out!

It keeps erroring on line 8

local character = game:GetService("Players").LocalPlayer.Character
local tool = script.Parent

local swordSlashOne = tool:WaitForChild("SwordSlash1")
local swordSlashTwo = tool:WaitForChild("SwordSlash2")
local swordSlashThree = tool:WaitForChild("SwordSlash3")

local swordSlashOneTrack = character.Humanoid:LoadAnimation(swordSlashOne)
local swordSlashTwoTrack = character.Humanoid:LoadAnimation(swordSlashTwo)
local swordSlashThreeTrack = character.Humanoid:LoadAnimation(swordSlashThree)

local swordCombo = 0
local nextActivate = os.clock()
local debounceTime = 1

tool.Activated:Connect(function()
	if os.clock() > nextActivate then
		nextActivate = os.clock() + debounceTime
		
		if swordCombo == 0 then
			swordCombo = 1
			swordSlashOneTrack:Play()
			
		elseif swordCombo == 1 then
			swordCombo = 2
			swordSlashTwoTrack:Play()
			
		elseif swordCombo == 2 then
			swordCombo = 0
			swordSlashThreeTrack:Play()
		end
	end
end)

Try adding a WaitForChild for Humanoid, it may be because the character has not fully loaded yet

The ‘character’ variable in your case is nil, so you should type:

local localPlayer = game:GetService("Players").LocalPlayer
local character = localPlayer.Character or localPlayer.CharacterAdded:Wait()

I’m sorry, I am rather new to scripting and am not understanding how to do that correctly. I tried a few different ways to possibly use that correctly just now and I must not be understanding the correct use of WaitForChild or something. Could you elaborate?

Yea sure. So where you’ve got this:
local swordSlashOneTrack = character.Humanoid:LoadAnimation(swordSlashOne)

replace it with:
local swordSlashOneTrack = character:WaitForChild("Humanoid"):LoadAnimation(swordSlashOne)

You could also use noname’s solution as that would likely solve your issue

WIki links:
https://developer.roblox.com/en-us/api-reference/class/Humanoid

I’m getting (Humanoid is not a valid member of model “Workspace.cjjjimmy”)

now it says attempt to index Nil with WaitForChild

Try the example given by noname

local localPlayer = game:GetService("Players").LocalPlayer
local character = localPlayer.Character or localPlayer.CharacterAdded:Wait()

I already did and replied to him, It didn’t work.

Humanoid is not a valid member of “Workspace.cjjjimmy”

Try this: (also, it’s fine to be a beginner)

--//Services
local Players = game:GetService("Players")

--//Variables
local LocalPlayer = Players.LocalPlayer
local Character = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")

local tool = script.Parent

local swordSlashOne = tool:WaitForChild("SwordSlash1")
local swordSlashTwo = tool:WaitForChild("SwordSlash2")
local swordSlashThree = tool:WaitForChild("SwordSlash3")

local swordSlashOneTrack = Humanoid:LoadAnimation(swordSlashOne)
local swordSlashTwoTrack = Humanoid:LoadAnimation(swordSlashTwo)
local swordSlashThreeTrack = Humanoid:LoadAnimation(swordSlashThree)

--//Controls
local swordCombo = 0
local nextActivate = os.clock()
local debounceTime = 1

--//Functions
tool.Activated:Connect(function()
	if os.clock() > nextActivate then
		nextActivate = os.clock() + debounceTime

		if swordCombo == 0 then
			swordCombo = 1
			swordSlashOneTrack:Play()

		elseif swordCombo == 1 then
			swordCombo = 2
			swordSlashTwoTrack:Play()

		elseif swordCombo == 2 then
			swordCombo = 0
			swordSlashThreeTrack:Play()
		end
	end
end)
1 Like

This worked! Also showed me some nice ways to make my code more readable, So thank you for that!!

1 Like

No problem, have a good day!

30