NPC can't be damaged by a certain part

Hi guys,NPCs are humanoids so they are damaged by the same things that damage players,how can I make a NPC that is immune to the damage caused by a certain part?(I need a method that can work with any type of NPCs) If it is helpful here’s the script of the part

– code
script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") and hit.Parent.HumanoidRootPart.BrickColor == BrickColor.new("Medium stone grey") then
        hit.Parent.HumanoidRootPart.BrickColor = BrickColor.new("Really red")
        
        
        hit.Parent.Humanoid:TakeDamage(35)

        hit.Parent.HumanoidRootPart.BrickColor = BrickColor.new("Medium stone grey")
    end
end)

You can insert a ForceField object into the NPC’s character.

info

With ForceFields, NPCs don’t take damage if you use :TakeDamage(), but if you change the property Health it will damage him, so use :TakeDamage()

You can as well check if the NPC’s character name doesn’t have a Player with the same name in Players.

if not(game.Players:FindFirstChild(hit.Parent.Name)) then
1 Like

I want the NPC to be immune to the damage dealt from a certain part but the NPC have to be killed by weapons

Well in that case, simply make the

if not(game.Players:FindFirstChild(hit.Parent.Name)) then

check in the parts that kill, and don’t in the weapons.

so after “then” I have to write the line of code in the part that kill you?

You need to the check after the first if statement, in fact that script will not work sometimes and will error, because if hit.Parent didn’t have a HumanoidRootPart that will error. You have to check that afterwards and separately, so you check for the HumanoidRootPart and if the character is not a player in the same statement. I also recommend you store hit.Parent in a variable because you’re using it a lit.

    local character = hit.Parent --replace all hit.Parents with this
    if hit.Parent:FindFirstChild("Humanoid") then
        if not(game.Players:FindFirstChild(hit.Parent.Name)) and hit.Parent.HumanoidRootPart.BrickColor == BrickColor.new("Medium stone grey") 
             hit.Parent.HumanoidRootPart.BrickColor = BrickColor.new("Really red")
        
        
             hit.Parent.Humanoid:TakeDamage(35)

             hit.Parent.HumanoidRootPart.BrickColor = BrickColor.new("Medium stone grey")
        end
    end
1 Like