Hello! I was trying to make an animated sword tool, everything works according to plan, but a weird phenomenon happens, I put a video below showing it:
I have done all the measures taken in other topics, however, it has still not worked.
My code stops ALL animations from occurring when the tool is unequipped, but it still lingers.
It only happens when the tool is unequipped when the equipTrack animation has not finished. It works fine if the idleTrack animation has started.
The code mentioned above is here, running in a LocalScript.
local player = game.Players.LocalPlayer
local animator = player.Character:WaitForChild("Humanoid"):FindFirstChildOfClass("Animator")
local equipTrack = player.Character:WaitForChild("Humanoid"):LoadAnimation(script.Equip)
local idleTrack = player.Character:WaitForChild("Humanoid"):LoadAnimation(script.Idle)
local swing1Track = player.Character:WaitForChild("Humanoid"):LoadAnimation(script.Swing1)
local swing2Track = player.Character:WaitForChild("Humanoid"):LoadAnimation(script.Swing2)
local cooldown = false
script.Parent.Equipped:Connect(function()
equipTrack:Play() --Play equipping animation
script.Parent.ToolEquipped:FireServer()
equipTrack.Stopped:Connect(function() --Wait until animation finishes
idleTrack:Play() --Play idle track
end)
end)
script.Parent.Unequipped:Connect(function()
for i,v in ipairs(animator:GetPlayingAnimationTracks()) do
v:Stop()
end
end)
script.Parent.Activated:Connect(function()
local randState = math.random(1,2)
if randState == 1 and cooldown == false then --Play swing animations at random
swing1Track:Play()
script.Parent.ToolActivated:FireServer() --Fire activation event
cooldown = true --Set cooldown to true
swing1Track.Stopped:Wait()
cooldown = false --Reset cooldown
script.Parent.ToolActivationComplete:FireServer()
elseif randState == 2 and cooldown == false then
swing2Track:Play()
cooldown = true --Same as above
script.Parent.ToolActivated:FireServer()
swing2Track.Stopped:Wait()
cooldown = false
script.Parent.ToolActivationComplete:FireServer() --Fire activation event
end
end)