Hello everyone, so today I made a script that plays an animation on a tool, when you click the animation plays, and we you unequip the tool, the animation stops. Heres the thing, if I click multiple times, the animation doesnt end, it just restart everytime I click. I would like to make it so you cant restart the animation till it ended, if anybody could help me, I would appreciate it, thank you!
Heres the script:
script.Parent.Equipped:Connect(function(Mouse)
Mouse.Button1Down:Connect(function()
local animation = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script.Parent.Animation)
animation:Play()
wait(1)
animation:Stop()
end)
end)
local animation = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script.Parent.Animation)
script.Parent.Unequipped:Connect(function()
animation:Stop()
end)
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local animator = humanoid:FindFirstChildOfClass("Animator")
if not animator then
animator = Instance.new("Animator")
animator.Parent = humanoid
end
local animation = animator:LoadAnimation(script.Parent:WaitForChild("Animation"))
script.Parent.Activated:Connect(function()
animation:Play()
wait(1)
animation:Stop()
end)
script.Parent.Unequipped:Connect(function()
animation:Stop()
end)
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local animator = humanoid:FindFirstChildOfClass("Animator")
if not animator then
animator = Instance.new("Animator")
animator.Parent = humanoid
end
local animation = animator:LoadAnimation(script.Parent:WaitForChild("Animation"))
local debounce = false
script.Parent.Activated:Connect(function()
if not debounce then
debounce = true
animation:Play()
wait(1)
animation:Stop()
debounce = false
end
end)
script.Parent.Unequipped:Connect(function()
animation:Stop()
end)
local Players = game:GetService("Players")
local tool = script.Parent
local animInstance = tool.Animation
local plr = Players.LocalPlayer
local mouse = plr:GetMouse()
local char = plr.Character
local animTrack = char:WaitForChild("Humanoid"):LoadAnimation(animInstance)
local ancestryChangedConn, equippedConn, unequippedConn, mouseConn
local function onClick()
if not animTrack.IsPlaying then
animTrack:Play()
wait(1)
animTrack:Stop()
end
end
local function onEquipped()
mouseConn = mouse.Button1Down:Connect(onClick)
end
local function onUnEquipped()
mouseConn:Disconnect()
animTrack:Stop()
end
local function onAncestryChanged(_, parent)
if not parent then
ancestryConn:Disconnect()
equippedConn:Disconnect()
unEquippedConn:Disconnect()
mouseConn:Disconnect()
end
end
equippedConn = tool.Equipped:Connect(onEquipped)
unEquippedConn = tool.UnEquipped:Connect(onUnEquipped)
ancestryChangedConn = char.AncestryChanged:Connect(onAncestryChanged)