So, I was messing around with my tool idle animation with scripts. I came across a problem where the idle animation would still play even when I unequipped the animation. Another problem I also found is that the idle animation doesn’t play right when I equip the tool but only after I activate the weapon. How can I fix these problems?
Heres my code:
local CanAttack = true
local player = game:GetService("Players").LocalPlayer
local Character = player.Character or player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
script.Parent.Equipped:Connect(function()
local idle = script.Parent.Parent.Humanoid.Animator:LoadAnimation(script.Idle)
idle:Play()
end)
script.Parent.Unequipped:Connect(function()
local idle = script.Parent.Parent.Humanoid.Animator:LoadAnimation(script.Idle)
idle:Stop()
end)
script.Parent.Activated:Connect(function()
local idle = script.Parent.Parent.Humanoid.Animator:LoadAnimation(script.Idle)
local attack = script.Parent.Parent.Humanoid.Animator:LoadAnimation(script.Attack)
if CanAttack == true then
attack:Play()
idle:Stop()
CanAttack = false
attack.Stopped:Wait()
attack:Stop()
idle:Play()
CanAttack = true
script.Parent.CanDamage.Value = true
end
end)
so well your loading the idle animation 3 times (multiplied by the number of times each event is fired), each time you load a new animation its a new animation not the same from earlier in the script
a simple fix, load the animation once outside the different functions
local CanAttack = true
local player = game:GetService("Players").LocalPlayer
local Character = player.Character or player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local idle = script.Parent.Parent.Humanoid.Animator:LoadAnimation(script.Idle)
local attack = script.Parent.Parent.Humanoid.Animator:LoadAnimation(script.Attack)
-- the idle animation fix also applies to the attack animation
script.Parent.Equipped:Connect(function()
idle:Play()
end)
script.Parent.Unequipped:Connect(function()
idle:Stop()
end)
script.Parent.Activated:Connect(function()
if CanAttack == true then
attack:Play()
idle:Stop()
CanAttack = false
attack.Stopped:Wait()
attack:Stop()
idle:Play()
CanAttack = true
script.Parent.CanDamage.Value = true
end
end)
also something else I noticed, if you were to attack and then before the attack finished unequip the weapon, then the idle animation would also play while the weapon is unequipped, I suppose a simple fix for this would be a boolean for equipped
local CanAttack = true
local player = game:GetService("Players").LocalPlayer
local Character = player.Character or player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local idle = script.Parent.Parent.Humanoid.Animator:LoadAnimation(script.Idle)
local attack = script.Parent.Parent.Humanoid.Animator:LoadAnimation(script.Attack)
local equipped = false -- bool
script.Parent.Equipped:Connect(function()
idle:Play(); equipped = true
end)
script.Parent.Unequipped:Connect(function()
idle:Stop(); equipped = false
end)
script.Parent.Activated:Connect(function()
if CanAttack == true then
attack:Play()
idle:Stop()
CanAttack = false
attack.Stopped:Wait()
attack:Stop()
if equipped == true then idle:Play() end -- check bool
CanAttack = true
script.Parent.CanDamage.Value = true
end
end)
Your loading an animation multiple times. Maybe define the AnimationTrack’s?
local player = game:GetService("Players").LocalPlayer
local Character = player.Character or player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local Animator = Humanoid:WaitForChild("Animator")
local idle = Animator:LoadAnimation(script.Idle)
local attack = Animator:LoadAnimation(script.Attack)
script.Parent.Equipped:Connect(function()
idle:Play()
end)
script.Parent.Unequipped:Connect(function()
idle:Stop()
end)
script.Parent.Activated:Connect(function()
if CanAttack == true then
attack:Play()
idle:Stop()
CanAttack = false
attack.Stopped:Wait()
attack:Stop()
idle:Play()
CanAttack = true
script.Parent.CanDamage.Value = true
end
end)