Idle animation problem

  1. **What do you want to achieve?I want achieve idle animation run only when tool is equipped.

  2. **What is the issue? issue is when i unnequip tool idle animation stays


    and here image where it stays

    how i tried to fix it?
    here is animation local script its inside tool

local Toll = script.Parent
local idleanim = Instance.new("Animation")
idleanim.AnimationId = "rbxassetid://7987783795"
local swinganim = Instance.new("Animation")
swinganim.AnimationId = "rbxassetid://7983245766"
local swinganim2 = Instance.new("Animation")
swinganim2.AnimationId = "rbxassetid://7987827486"


Toll.Equipped:Connect(function(Animation1)
	local Animation1 = script.Parent.Parent.Humanoid:LoadAnimation(idleanim)
	Animation1.Priority = Enum.AnimationPriority.Movement
	Animation1:Play() while true do
		wait(0.2)
		print("Looping")
		if Toll.Unequipped then
			break
		end
	end
end)



Toll.Activated:Connect(function()
	local swing1 = script.Parent.Parent.Humanoid:LoadAnimation(swinganim)
	local swing2 = script.Parent.Parent.Humanoid:LoadAnimation(swinganim2)
	swing1:play()
	wait(0.5)
	swing1:stop()
	wait(0.01)
	swing2:Play()
	wait(0.5)
	swing2:Stop()
end)

Toll.Unequipped:Connect(function()
  idleanim:Stop()
end)

to fix it i watched many tutorials about idle animations and tool animation and seams like none of theme worked

Sorry for my Bad English.
its my first post
and i am very new to Roblox scripting i only started scripting 3 days ago

and if i posted something incorrect and against the rules i am so sorry

1 Like

This is because you are referencing the animation as its name idleanim when its still named Animation1 in the script. So instead it would be this:

local Toll = script.Parent
local idleanim = Instance.new("Animation")
idleanim.AnimationId = "rbxassetid://7987783795"
local swinganim = Instance.new("Animation")
swinganim.AnimationId = "rbxassetid://7983245766"
local swinganim2 = Instance.new("Animation")
swinganim2.AnimationId = "rbxassetid://7987827486"


Toll.Equipped:Connect(function(Animation1)
	local Animation1 = script.Parent.Parent.Humanoid:LoadAnimation(idleanim)
	Animation1.Priority = Enum.AnimationPriority.Movement
	Animation1:Play() while true do
		wait(0.2)
		print("Looping")
		if Toll.Unequipped then
			break
		end
	end
end)



Toll.Activated:Connect(function()
	local swing1 = script.Parent.Parent.Humanoid:LoadAnimation(swinganim)
	local swing2 = script.Parent.Parent.Humanoid:LoadAnimation(swinganim2)
	swing1:play()
	wait(0.5)
	swing1:stop()
	wait(0.01)
	swing2:Play()
	wait(0.5)
	swing2:Stop()
end)

Toll.Unequipped:Connect(function()
  Animation1:Stop()
end)

Also it is very inefficient to use a while true do loop for looping an animation when you can set an animation to looping in its properties or the animation itself.

I closed function equipped so function uneqquip dont know what is Animation1

again sorry for my English

Remove the local when defining Animation1 in the equip script to fix this, also why are you loop checking if the tool is unequipped in the equip function? That is completely pointless since you have a function when the tool unequips already.

Here is what the script should look like, sorry if I came off as rude, but welcome to the devforum!

local Toll = script.Parent
local idleanim = Instance.new("Animation")
idleanim.AnimationId = "rbxassetid://7987783795"
local swinganim = Instance.new("Animation")
swinganim.AnimationId = "rbxassetid://7983245766"
local swinganim2 = Instance.new("Animation")
swinganim2.AnimationId = "rbxassetid://7987827486"


Toll.Equipped:Connect(function(Animation1)
	Animation1 = script.Parent.Parent.Humanoid:LoadAnimation(idleanim)
	Animation1.Priority = Enum.AnimationPriority.Idle
        Animation1:Play()
	end
end)



Toll.Activated:Connect(function()
	local swing1 = script.Parent.Parent.Humanoid:LoadAnimation(swinganim)
	local swing2 = script.Parent.Parent.Humanoid:LoadAnimation(swinganim2)
	swing1:play()
	wait(0.5)
	swing1:stop()
	wait(0.01)
	swing2:Play()
	wait(0.5)
	swing2:Stop()
end)

Toll.Unequipped:Connect(function()
  Animation1:Stop()
end)

Please reply if you have any errors, and if this work I would really appreciate if you marked this as the solution.

1 Like

just copy paste this in and you’re good to go

--do note that tool animations dont replicate to the server

local player = game.Players.LocalPlayer
local idleAnimPlayer

local Anim = --reference the animation here, save it in replicated storage.

Tool.Equipped:Connect(function()
      idleAnimPlayer = player.Character.Humanoid.Animator:LoadAnimation(Anim)
      idleAnimPlayer:Play
      idleAnimPlayer.Looped = true
end)

Tool.Unequipped:Connect(function()
      idleAnimPlayer:Stop()

end)

Thank you so much it worked!!
I guess i need to watch more tutorials to understand lua