Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Adding a cooldown and animation for NPC attacks?

Hey everyone,

So currently I have a very basic attack script for when my NPC touches the player. I've tried using a simple wait(2) to see if that will slowdown the attack but I think its alittle more complicated then I realize.

I was also planning to work in an Animation later that would be effected by this cooldown.

Here is my basic Attack Script. Could anyone offer advice on how to setup a cooldown so the attack isnt a constant spam of damage?

local debounce = true

  

function onTouched(hit)

local human = hit.Parent:findFirstChild("Humanoid")

if (human ~= nil) and debounce then

debounce = false

wait(20)

human.Health = human.Health - 1

end

end

script.Parent.Touched:connect(onTouched)

1 comment

0

Accept PhantomVisual's answer, they both are the same, but his actually provides a clear explanation.

2 answers

✓ Accepted answer

Uh so just wrote this up, didn't have the chance to test it but hopefully it works, comment if there's any errors and I'll update the script.

local cooldown = 0

script.Parent.Touched:Connect(function(hit)
local human = hit.Parent:findFirstChild("Humanoid")

if (human ~= nil) and cooldown ==  0 then
human.Health = human.Health - 1
repeat 
       cooldown = cooldown+1
       wait(1)
until wait >= 20
cooldown = 0

end
end)

So this is a learning experience, debounce works usually with a simple true/false situation. Eg; Open/Close UI. So you just gotta get creative and learn what works for what best, usually browsing the wiki is a good way to learn alternatives.

4 comments

0

Ohhhh reading over that it makes alot of sense. I definitely need to be more flexible with the way I approach these problems, thanks for the fast reply will test in abit

0

No problem, if it works just mark the question as answered :)

0

So ive tried this out and mostly got it working, sadly I run into an error with line 11

0

until cooldown >= 20

You seem to know how debounces work, but you aren't applying them correctly.

local debounce = true

Part.Touched:Connect(function(part)
    local hum = part.Parent:FindFirstChildOfClass('Humanoid')
    if (debounce) and (hum) then
	    debounce = false
	    hum:TakeDamage(1)
	    wait(1)
	    debounce = true
    end
end)

Lets go over what will happen. debounce is set to true. Once this part goes into contact with something, it'll check if it's a humanoid and if debounce is set to true.

Lets say it does touch a Humanoid. debounce is true and hum is existing, making it pass through the if statement. Then debounce is set to false. That means when the touched triggers, it won't get pass that if statement because debounce is set to false. This will allow it to trigger once.

But what if we want it to trigger again? We add a wait() and set the debounce back to true so it can get pass the if statement to trigger again.

This discussion is preserved as it was. New answers, comments, and votes are closed.