LoadAnimation requires an Animation object even though it exists

Hello. I’m currently working on a tool the plays an animation on equipping. While the code runs as is, I’m getting this error:

image

The animation is an actual instance under the LocalScript of the tool. Wondering what is possibly causing the issue. Here is the code:

-- Get services
local Players = game:GetService("Players")

-- Get player
local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local characterValue = player:WaitForChild("Character#")

-- Get tool
local glock = script.Parent

-- Get animations
local idleAnimation = script:FindFirstChild("Idle")
local idleAnimationTrack = nil

character.ChildAdded:Connect(function(child)
	if child:IsA("Tool") and child.Name == "Glock" then
		if not character then
			return
		end

		local humanoid = character:FindFirstChild("Humanoid")
		
		if not humanoid then
			return
		end

		local animator = humanoid:FindFirstChild("Animator")
		
		if not animator then
			return
		end

		idleAnimationTrack = animator:LoadAnimation(idleAnimation)
		idleAnimationTrack:Play()
	end
end)

character.ChildRemoved:Connect(function(child)
	if child:IsA("Tool") and child.Name == "Glock" then
		if idleAnimationTrack then
			idleAnimationTrack:Stop()
		end
	end
end)
1 Like

maybe the variable for “idleAnimation” is = nil. this is because you either spelt the animation name wrong, or there is another instance with the name “Idle” inside the script.

local idleAnimation = script:FindFirstChild("Idle") -- check name and check instance

Try using script:WaitForChild("Idle"). It might be the animation hasn’t loaded yet, especially considering it runs as one of the first few lines of code in the script.

Could please show the explorer to clarify that it is truly an Animation object and not CurveAnimation or something?

Are you referring to idleAnimationTrack = nil? Because that becomes defined when the player uses the tool. Also I checked for any spelling errors and any additional instances with that name. I didn’t see anything.

Just tried it. The output now says there is an infinite yield waiting for the animation.

image

-- Get services
local Players = game:GetService("Players")

-- Get player
local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
local characterValue = player:WaitForChild("Character#")

-- Get tool
local glock = script.Parent

-- Get animations
local idleAnimation = script:WaitForChild("Idle")
local idleAnimationTrack = nil

character.ChildAdded:Connect(function(child)
	if child:IsA("Tool") and child.Name == "Glock" then
		if not character then
			return
		end

		idleAnimationTrack = animator:LoadAnimation(idleAnimation)
		idleAnimationTrack:Play()
	end
end)

character.ChildRemoved:Connect(function(child)
	if child:IsA("Tool") and child.Name == "Glock" then
		if idleAnimationTrack then
			idleAnimationTrack:Stop()
                        idleAnimationTrack = nil
		end
	end
end)

try this? maybe it will work, you just gotta get the Humanoid at the first place. (edited)

Make sure the Animation.Archivable property is set to true, I just managed to reproduce this issue by disabling it.

This didn’t work. The output tells me there is an infinite yield for script:WaitForChild("Idle") unfortunately. I switched it back to FindFirstChild() without any luck either.

It is currently set to true. (filler text)

That’s strange. Are you sure you have no other scripts that edit the animation’s parent or modify it’s Archivable property?

As a workaround, you could create the animation from within your script.

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