NPC taking damage from there own move

Hey! I have ran into a problem, my NPC is taking damage from it’s own move. Here is the code:

HitDamage = 8

local Duration = false

function onTouched(part)
local Person = part.Parent:findFirstChild(“Humanoid”)
local TheirTorso = part.Parent:findFirstChild(“Torso”)
if Person~=nil and Duration == false then
Person:TakeDamage(HitDamage)
wait(1)
Duration = true
end
end
script.Parent.Touched:connect(onTouched)

I think if the humanoid is able to take damage 2 seconds after it is created, it will fix up the problem, but idk how to do it. Thanks for reading!

You need to add another condition in your if statement

In this, you need a variable to identify the NPC

if NPC  ~= Person and Person~=nil and Duration == false then

ZDH_Dev contacted me about the issue, and we have resolved most of the issues that occurred with the NPC.
Thanks for helping :slightly_smiling_face:
EDIT: The issues didn’t have to do much with the script, rather the issue was the other NPCs in the game.

you aren’t detecting if the humanoid is the npc’s own humanoid, so do this

(put the script inside the npc model btw)

HitDamage = 8

local Duration = false

function onTouched(part)
    local Person = part.Parent:FindFirstChildWhichIsA("Humanoid")
    local TheirTorso = part.Parent:FindFirstChild("Torso")
    if Person~=nil and Duration == false then
        if Person == script.Parent:FindFirstChildWhichIsA("Humanoid") then return end -- just add this part
        Duration = true
        Person:TakeDamage(HitDamage)
        task.wait(1) -- task.wait is better than wait. (use task.wait(0.03) to replace wait())
        Duration = false -- fixed this broken cooldown lol
    end
end

script.Parent.Touched:connect(onTouched)
1 Like