Debounce monotony

Hello forum members,

I’ve been trying to make a simple enemy framework. It works fine, but there’s this piece of code that I have to copy and paste all the time for cooldowns and it gets annoying to look at.

example a:

-- FSM; actually easier; can make statuses classes if you want to for better functionality
if self._status == ENUM_STATUSES.Idle and not self.Calculating then
    self:__GetPath()
    self.Calculating = true
    delay(self.Smartness, function()
        self.Calculating = false
    end)
end

example b:

elseif DistanceFromTarget < MOVE_WAY_DIST and not self.Attacking then -- close enough to damage
    self.TargetHum.Health -= self.Strength
    self.Attacking = true
    delay(self.Smartness, function()
        self.Attacking = false
    end)
end

I’m not sure what they are called (debounces??) but they just seem to clutter my code. What can I do to replace this piece of code? Please leave below alternatives. Thanks for reading.

You might be able to make a function out of it or something if your personal preference suits that

function debounce(func)
    func()
    self.Calculating = true
    delay(self.Smartness, function() self.Calculating = false end)
end

--example of use
debounce(function()
    if bread then
        Instance.new("Explosion",self.TargetHum.Parent.HumanoidRootPart)
    end
end)

Still kinda cluttery though but this is just an idea (you could even add a variable for the debounce variable in the debounce function like

debounce(self.Attacking,function()…