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