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

How to make an enemy NPC attack ?

I m trying to make an enemy NPC load a punch animation when he is close to the player. So to decide when the NPC start punching, I put a part all around the NPC and when the player touched it is supposed to load the animation.My problem is that the animation constantly plays when the player touch the part. So I want to know how to pause between each time the animation play? or if someone can shows me a better way to make the NPC attack it would be appreciated ! Here is my script

local hum = script.Parent.Parent:WaitForChild("Humanoid") local anim = hum:LoadAnimation(script:FindFirstChildOfClass("Animation"))

local function onTouch(hit) if (hit.Parent:FindFirstChild('Humanoid')~= nil) then anim:Play() wait(1) end end

script.Parent.Touched:Connect(onTouch)

1 comment

0

Format your code by pressing the lua button in text editor.

1 answer

✓ Accepted answer

debounce, my friend.

local debounce = true
local hum = script.Parent.Parent:WaitForChild("Humanoid") 
local anim = hum:LoadAnimation(script:FindFirstChildOfClass("Animation"))
local DAMAGE = 15 --Feel free to change this

function onTouch(hit) 
	local enemy_hum = hit.Parent:FindFirstChild("Humanoid")
	if enemy_hum and debounce then --Checks if debounce is true and if the hit's parent has a humanoid
		debounce = false --Turns debounce to false
		enemy_hum:TakeDamage(DAMAGE) --Humanoids have a :TakeDamage function
		anim:Play() 
		wait(1) 
		debounce = true --Waits 1 second before debounce is true
	end
end

script.Parent.Touched:Connect(onTouch)

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