Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to make an enemy NPC attack ?

Asked by 4 years ago

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)

0
Format your code by pressing the lua button in text editor. RunKittenzRComin 170 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

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)
Ad

Answer this question