Animation Support

Hey! I’m trying to place this animation script into a tool that when clicked, runs the animation, but I don’t want players to be able to spam the animation; I want them to wait until the animation has fully played before being able to cue the animation again. Any suggestions given the current code?

Tool = script.Parent
local plr1 = game.Players:FindFirstChild(script.Parent.Parent.Name)
local player
if not plr1 then
	local plr2 = game.Players:FindFirstChild(script.Parent.Parent.Parent.Name)
	if plr2 then
		player = plr2
	end
else
	player = plr1
end

repeat wait() until player.Character ~= nil
local hum = player.Character:WaitForChild("Humanoid")

local animation = Tool.Animation 
local AnimTrack = hum:LoadAnimation(animation) 

Tool.Equipped:connect(function(mouse) 
    mouse.Button1Down:Connect(function() 
		AnimTrack:Play()
    end)
	Tool.Unequipped:connect(function() 
	    AnimTrack:Stop()
	end)
end)

Thank you!!

You’re able to use two things; Debounce and AnimationTrack.Ended().

Debounce is basically just cooldown, this is an example:

local Tool = script.Parent
local DB = false

Tool.Activated:Connect(function()
	if not DB then
		DB = true
		
		print("Hello!")
		task.wait(1) --Change the number to how many seconds it takes for the Debounce to last
		DB = false
	end
end)

As for AnimationTrack.Ended() or simply .Ended() for short, this is an event (I think) which fires whenever the animation ends. We can modify the example script above into this:

local Tool = script.Parent
local Humanoid = Tool.Parent:WaitForChild("Humanoid")
local DB = false

Tool.Activated:Connect(function()
	if not DB then
		DB = true
		
		local AnimationTrack = Humanoid:WaitForChild("Animator"):LoadAnimation(Tool.Animation)
		AnimationTrack:Play()
		
		AnimationTrack.Ended:Wait() --This will ONLY fire once the AnimationTrack ends
		
		DB = false
	end
end)

Now, in my own personal experience, this is a bit slightly janky I think although is better than simply just using task.wait(Insert many seconds it takes the animaton is going to last). (Sorry if the sentence is kinda sloppy, I didn’t have anything better to type.)

Where abouts in my script would the debounce function go to also ensure mouse.Button1Down still works?

What do you mean by that exactly?

As Krip said, you can use Debounce and AnimationTrack.Ended. If you want to keep the same structure you already had, then you can do this:

--You can define the debounce variable on a different line as long as it is defined before Tool.Equipped
local debounce = false 

Tool.Equipped:connect(function(mouse) 
    mouse.Button1Down:Connect(function() 
		if(not debounce) then
			debounce = true

			AnimTrack:Play()
			AnimTrack.Ended:Wait() --Wait until the animation is completed

			debounce = true
		end
    end)
	Tool.Unequipped:connect(function() 
	    AnimTrack:Stop()
		--Set debounce to false in case the user unequips the tool before the animation is completed
		debounce = false 
	end)
end)