Attempt to index nil with 'LoadAnimation'

  1. What do you want to achieve? Keep it simple and clear!
    I want to make a sword combat system.

  2. What is the issue? Include screenshots / videos if possible!
    The issue is that I get the error: attempt to index nil with 'LoadAnimation'

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I tried to look for solutions on devForum, but for some reason they didn’t work for me or I am missing something.

The problem is happening in the Local Script

-- Services
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local StarterGui = game:GetService("StarterGui")
local UserInputService = game:GetService("UserInputService")

-- Modules
local Config = require(script.Parent.Parent.Modules.Config)

-- Tool
local Tool = script.Parent.Parent
local Handle = Tool:WaitForChild("Handle")

-- UI
local UI = Tool.Assets.UI
local CooldownUI = UI.CooldownUI

-- Sword Vars
local Damage = Config.Damage
local Cooldown = Config.Cooldown

-- Player Vars
local player, humanoid, char
char = Tool.Parent
player = Players:GetPlayerFromCharacter(char)
humanoid = char:FindFirstChild("Humanoid")

-- Anim Folder
local AnimFolder = Tool.Animations

-- Booleans
local isAttacking = false
local canDamage = true

-- Animation Instances

for i,v in pairs(Config.Animations.SlashAnimations) do
	if not v.Enabled then continue end
	
	local Anim = Instance.new("Animation")
	Anim.Parent = AnimFolder
	Anim.Name = i
	Anim.AnimationId = v.AnimationId
end

for i,v in pairs(Config.Animations.OtherAnimations) do
	if not v.Enabled then continue end
	
	local Anim = Instance.new("Animation")
	Anim.Parent = AnimFolder
	Anim.Name = i
	Anim.AnimationId = v.AnimationId
end


local function onEquip()
	local EquipAnim = AnimFolder:WaitForChild("Equip")
	local EquipTrack = humanoid:LoadAnimation(EquipAnim)
	EquipTrack:Play()
end

-- Attack
local Combo = 1

local function Attack()
	local Slash = humanoid:LoadAnimation(AnimFolder:WaitForChild("Slash" .. tostring(Combo)))
	Slash:Play()
	
	Tool.Remotes.ClientCast:FireServer(Slash.Length, Config.Cooldown)
	canDamage = false
	task.wait(math.max(Slash.Length, Config.Cooldown))
	canDamage = true
end

UserInputService.InputBegan:Connect(function(input, gameProccessed)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		if char:FindFirstChild(Tool.Name) and char[Tool.Name]:IsA("Tool") then
			if isAttacking == false and canDamage then
				isAttacking = true

				if Combo > 5 then
					Combo = 1
				end

				Attack()

			--[[local Slash1Track = humanoid:LoadAnimation(AnimFolder:WaitForChild("Slash1"))
			Slash1Track:Play()
			Caster:Start()
			Caster.HumanoidCollided:Connect(function(result, hithumanoid)
				if db[hithumanoid] then return end

				db[hithumanoid] = true

				hithumanoid:TakeDamage(Config.Damage)

				wait(.2)

				db[hithumanoid] = false
			end)
			Caster:Stop()
			wait(math.max(Slash1Track.Length, Config.Cooldown))]]

				isAttacking = false
				Combo += 1
			
			end
		elseif not char:FindFirstChild(Tool.Name) and not char:FindFirstChildWhichIsA("Tool") then
			return
		end
	end
end)

Tool.Equipped:Connect(onEquip)

Instead of using this, use this:

local humanoid = game.Players.LocalPlayer.Character:WaitForChild("Humanoid")

PS: You may need to add game.Players.LocalPlayer.CharacterAdded:Wait() At the start of the script.

image
I made it like that.

image
I get infinite yield though.

Right now you’re using Tool.Parent, which will get the backpack, unless the tool is equipped. (Which it isn’t when the script runs) I would use Tool.Parent.Parent if you want to use that, or delete that part entirely.

If this is a local script use

game.Players.LocalPlayer

If not, When the tool gets equipped

tool.equipped:Connect(function()

end)

Set the player to

tool.Parent


It again doesn’t work. Also I tried removing the Tool.Parent.Parent but it still didn’t work.

Does it work if you replace all of that with the code in my first post?

I will need to use the char to check if the character has the tool equipped or not though.

Why don’t you use Tool.Equipped for that?

Because I do not want to use an event for that.
image
I think this image will help you understand why.

Would something like this work for you?

Tool.Equipped:Connect(function()
	local toolEquipped = true
end)
Tool.Unequipped:Connect(function()
	local toolEquipped = false
end)

And you could use use if toolEquipped then instead of FindFirstChild(“Tool.Name”)

No because the player will need to unequip the tool so I can check if he doesn’t have the tool equipped.

Though, the humanoid now works but I can’t do a slash animation with the sword.