My animation is spamable

I wanna make my eating animation

I can spam the animation when I shouldn’t be able to because then it makes a non fatal error.
(sorry for the lag)

Google and DevForum

I am trying to make this happen on the client or else this wont work. I am trying to check if the animation is playing so I cannot make it spamable but when I do try this, I can still spam it, over, over and over again. This ‘bug’ does not cause any errors. [Not important just Disclaimer]: Then I get the error “Attempt to index nil with ‘Destroy’.” because my animation stops then plays again then when it stops it tries to destroy the part.

--// This is where the glitch/bug happens
if  itemName2.Name == itemName.Name and eatingAnim.IsPlaying == false then 
   eatingAnim:Play()
					
   eatingAnim:GetMarkerReachedSignal("EatSound"):Connect(function() 
       SoundEffect:FireServer()
   end)
					
   eatingAnim.Stopped:Wait()
   EatEvent:FireServer(itemName2)
end

I have tried everything I could, I cannot find any solutions.

If you have any information please help me with this.

just use debounce

local debounce = false -- set debounce earlier in the script



if debounce == false and itemName2.Name == itemName.Name and eatingAnim.IsPlaying == false then 
   debounce = true
   eatingAnim:Play()
					
   eatingAnim:GetMarkerReachedSignal("EatSound"):Connect(function() 
       SoundEffect:FireServer()
   end)
					
   eatingAnim.Stopped:Wait()
   EatEvent:FireServer(itemName2)
   debounce = false
end

Waiting for the animation to stop isn’t enough alone. You need to make it so that after the animation stops, you set a variable called canEat to true. This is what it would look like:

local canEat = true

if  itemName2.Name == itemName.Name and eatingAnim.IsPlaying == false and canEat then
   canEat = false

   eatingAnim:Play()
					
   eatingAnim:GetMarkerReachedSignal("EatSound"):Connect(function() 
       SoundEffect:FireServer()
   end)
					
   eatingAnim.Stopped:Wait()
   EatEvent:FireServer(itemName2)
   canEat = true
end

Maybe add a debounce time? I dont really know how to fix this tho