LoadAnimation requires an animation object

I’ve been adding Animations for a pickaxe model and when I scripting in the mining animation, I noticed it wasn’t playing when the player used the tool. I also noticed this error in my output

Screenshot 2021-07-07 210333

I don’t know if I’m limited to the number of animations I can play at once or if it’s something else. I thought it was the Animation’s Name and a function’s name being the same, so I changed the Animation’s name but it still gave me the same error and never worked. Here’s what the pickaxe Looks like in the workspace.

image

here’s the script as well:

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

--Variables
local animations = {
	script.Parent.Animations.Idle,  --Idle Animation
	script.Parent.Animations.Walk, --Walking Animation
	script.Parent.Animations.Run, --Sprinting Animation
	script.Parent.Animations.Hit --Mining Animation
}
local loadedAnimations = {}
local tool = script.Parent
local hum = player.Character:WaitForChild("Humanoid")
local animator = hum:WaitForChild("Animator")
local miningSpeed = tool:WaitForChild("MiningSpeed").Value
local debounce = false
local ToolHeld = false

--Functions
function Update()
	if ToolHeld == true then
		if not loadedAnimations[2] then
			loadedAnimations[2] = animator:LoadAnimation(animations[2])
		end

		if hum.Health > 0 and hum.MoveDirection.Magnitude ~= 0 then
			if not loadedAnimations[2].IsPlaying then
				loadedAnimations[2]:Play()
			end
		else
			if loadedAnimations[2].IsPlaying then
				loadedAnimations[2]:Stop()
			end
		end
	end
end

local function Mine()
	if debounce == false then
		debounce = true
		if(not loadedAnimations[4]) then
			loadedAnimations[4] = animator:LoadAnimation(loadedAnimations[4])
		end
		loadedAnimations[4]:Play()
		tool.Mining.Value = true
		wait(0.1)
		tool.Mining.Value = false
		wait(miningSpeed)
		debounce = false
	end
end

--Set Up
RunService.RenderStepped:Connect(Update)
	
tool.Equipped:Connect(function(mouse)
	if not loadedAnimations[1] then
		loadedAnimations[1] = animator:LoadAnimation(animations[1])
	end
	ToolHeld = true
	loadedAnimations[1]:Play()
end)

tool.Activated:Connect(Mine)

tool.Unequipped:Connect(function()
	loadedAnimations[1]:Stop()
	if(loadedAnimations[4])then
		loadedAnimations[4]:Stop()
	elseif(loadedAnimations[2])then
		loadedAnimations[2]:Stop()
	end
	ToolHeld = false
end)

Any help will be greatly appreciated

Edit: Note that “loadedAnimations[4]” is the mining animation, that isn’t working

change this to:

:LoadAnimation(animations[4])
1 Like

local player = game:GetService(“Players”).LocalPlayer

local RunService = game:GetService(“RunService”)

local tool = script.Parent

local hum = player.Character:WaitForChild(“Humanoid”)

local animator = hum:WaitForChild(“Animator”)

local miningSpeed = tool:WaitForChild(“MiningSpeed”).Value

local debounce = false

local ToolHeld = false

local loadedAnimations = {

hum:LoadAnimation(script.Parent.Animations.Idle),

hum:LoadAnimation(script.Parent.Animations.Walk),

hum:LoadAnimation(script.Parent.Animations.Run),

hum:LoadAnimation(script.Parent.Animations.Hit)

}

–To use an animation make these:

loadedAnimations[1]:Play()

1 Like

It works now! Thank you so much for your help, i feel a bit dumb for missing that lol. But thanks so much!

1 Like