How can I make the animation stop spamming?

  1. What do you want to achieve? a punch animation that’s un-spammable

  2. What is the issue? Not really sure how to do it (here’s a video of it)

  3. What solutions have you tried so far? I’ve tried looking around in the internet but nothing was helpful

here’s the code ( it has parts taken from other scripts and modified to work for me )

local canPunch = true
local CurrentlyPunching = false
local Keybind = "F"
local damage = 15

local UIS = game:GetService("UserInputService")

UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.F then
		local anim = script.Parent.Parent.Humanoid:LoadAnimation(script.Animation)
		anim:Play()
		CurrentlyPunching = true
		wait(1)
		CurrentlyPunching = false
	end
end)

script.Parent.Parent.RightHand.Touched:connect(function(hit)
	local model = hit:FindFirstAncestorOfClass("Model")
	if model and model:FindFirstChild("Humanoid") and canPunch and CurrentlyPunching then
		model:FindFirstChild("Humanoid"):TakeDamage(damage)
		canPunch = false
		wait(1)
		canPunch = true
	end
end)

It’s called a “debounce”, and the premise is simple. Basically you add a variable, defaulted to false/true, that inverts itself when the action is started, then reverts to original after a specified period of time. For example:

local tool = script.Parent
local db = false

tool.Activated:Connect(function()
    if db then return end
    db = true
    -- do tool action
    task.wait(2)
    db = false
end)
2 Likes

yeah I know a bit about debounces but I don’t know how to make it work for an animation.

Throw it in here:

local db = false
UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.F and not db then
        db = true
		local anim = script.Parent.Parent.Humanoid:LoadAnimation(script.Animation)
		anim:Play()
		CurrentlyPunching = true
		task.wait(1)
		CurrentlyPunching = false
        db = false
	end
end)