Idle animation problems

I have a tool that has an Idle animation, that plays once you are holding the tool.
image

The problem is i can’t make it so when you unequip the tool it stops the animation.
image

This is my current script, any help would be appreciated:

local players = game:GetService("Players")
local plr = players.LocalPlayer.Character

local Animation = script.Parent:WaitForChild("Animation")

script.Parent.Equipped:Connect(function()
	local humanoid = script.Parent.Parent:WaitForChild("Humanoid")
	local loadAnim = humanoid.Animator:LoadAnimation(Animation)
	
	loadAnim:Play()
end)

script.Parent.Unequipped:Connect(function()
	local humanoid = plr.Humanoid
	local loadAnim = humanoid.Animator:LoadAnimation(Animation)
	
	loadAnim:Stop()
end)

Additionally, i also have a different animation that plays when i click the tool which is not working.

This is the script:

local cooldown = false
local Animation = script.Parent.Swing
local plr = game.Players.LocalPlayer

script.Parent.Activated:Connect(function()
	if cooldown == false then
		local humanoid = plr.Character.Humanoid
		local loadAnim = humanoid.Animator:LoadAnimation(Animation)
		
		loadAnim:Play()
		cooldown = true
	elseif cooldown == true then
		wait(2)
		cooldown = false
	end
end)

Output says:

LoadAnimation requires an Animation object

Even though i am clearly using an Animation Object
image

the loadanim does not point to the same object. Because this is a local script put loadanim out of the functions and set it only for when equipped.

1 Like

Could you explain that a little better? I didn’t understand it.

If you want all players to see the animation, you should load it from the server side (i think) with a script in the serverscriptservice (maybe with a remote event controlled by the tool)

That is not the issue i am going through.

try this

local players = game:GetService("Players")
local plr = players.LocalPlayer.Character

local Animation = script.Parent:WaitForChild("Animation")
local loadAnim = nil

script.Parent.Equipped:Connect(function()
	local humanoid = script.Parent.Parent:WaitForChild("Humanoid")
	loadAnim = humanoid.Animator:LoadAnimation(Animation)
	
	loadAnim:Play()
end)

script.Parent.Unequipped:Connect(function()
	if loadAnim then
        loadAnim:Stop()
    end
end)

the other script:

local enabled = true
local Animation = script.Parent.Swing
local plr = game.Players.LocalPlayer

script.Parent.Activated:Connect(function()
	if enabled then
        enabled = false
		local humanoid = plr.Character.Humanoid
		local loadAnim = humanoid.Animator:LoadAnimation(Animation)
		
		loadAnim:Play()
        wait(2) -- delay
		enabled = true
	end
end)
2 Likes

First one works, but the second one still has the same output error

LoadAnimation requires an Animation object

(For the second script)

You need to rename the animation. Rename it “SwingAnimation”, and reference it with the new name. The problem is that you have 2 things with the same name (an animation and a script), so the script thinks you mean the script names “Swing”, not the animation named “Swing”.

2 Likes

are you sure the object named “swing” is an animation and not an animation controller?

EDIT: (the solution is what XdJackyboiiXd21 says)

Changed it.

SwingAnimation is not a valid member of Tool "Players.Faczki.Backpack.Picareta"

image

You need to add waitforchild, so that it has time to load in.

local Animation = script.Parent:WaitForChild("SwingAnimation")
1 Like

Thank you for giving me the solution, but I think that @richiitalia deserves it because he did most of the work. Could you give him the solution.

2 Likes