I’m trying to make a script where when i equip a tool, an idle animation plays if im not running. It works, but what I’m trying to do is for the animations to stop when i unequip the tool, but it doesnt work. Here is an attempt that I’ve done but don’t work.
Tool.Equipped:Connect(function()
equip = true
local anim = script.Parent.AnimationIdle
local char = Tool.Parent
local Humanoid = char.Humanoid
local track = Humanoid:LoadAnimation(anim)
local trackt = Humanoid:LoadAnimation(animr)
Humanoid.Running:Connect(function(value)
if value > 0 and equip == true then
moving = true
track:Stop()
trackt:Play()
elseif value <0 and equip == true then
moving = false
trackt:Stop()
track:Play()
end
end)
end)
Tool.Unequipped:Connect(function()
moving = false
equip = false
end)
I can’t figure out any other way to do this, can anyone help?
You could unload it by using Humanoid.GetPlayingAnimationTracks and stopping it based on its name. I honestly prefer to handle it a different way but that involves a different code structure altogether. I don’t usually shove everything into the function listening to Equipped.
Try putting: elseif value <= 0 and equip == true then
Because the value shouldn’t ever go under 0, when you stop running it should return 0 instead. But just in case you can check for both lower than and equal to 0.
This should fix your issues, let me know if there is still an error:
local anim = script.Parent.AnimationIdle
local character = Tool.Parent
local Humanoid = character.Humanoid
local track = Humanoid:LoadAnimation(anim)
local trackt = Humanoid:LoadAnimation(animr)
Tool.Equipped:Connect(function()
equip = true
Humanoid.Running:Connect(function(value)
if value > 0 and equip == true then
moving = true
track:Stop()
trackt:Play()
elseif value <= 0 and equip == true then
moving = false
trackt:Stop()
track:Play()
end
end)
end)
Tool.Unequipped:Connect(function()
moving = false
equip = false
trackt:Stop()
track:Stop()
end)
There were 2 things to fix in the script for it to work, and this is was one of the two. Thanks for helping
Here’s the script:
Tool = script.Parent
local char = script.Parent.Parent.Parent.Character
anim = script.Parent.AnimationIdle
animr = script.Parent.Run
local Humanoid = char.Humanoid
moving = false
local equip = false
--local anim = script.Parent.AnimationIdle
local char = Tool.Parent
Tool.Equipped:Connect(function()
local trackt = Humanoid:LoadAnimation(animr)
trackt:Play()
equip = true
end)
Humanoid.Running:Connect(function(value)
local track = Humanoid:LoadAnimation(anim)
local trackt = Humanoid:LoadAnimation(animr)
if value >= 1 and equip == true then
moving = true
track:Stop()
trackt:Play()
elseif value <= 0 and equip == true then
moving = false
trackt:Stop()
track:Play()
end
Tool.Unequipped:Connect(function()
equip = false
if track.IsPlaying then
track:Stop()
end
if trackt.IsPlaying then
trackt:Stop()
end
end)
end)
You should work on indenting your code properly for the sake of readability and tackling issues. Your current code creates a function and has it listen to Tool.Unequipped every time the function listening to Humanoid.Running is called. One of the major issues this presents is a memory leak, which can lower game performance.