How to make an npc not killable?

So my friend and I are making a zombie game. We put two NPC’s of our selves in, but the problem is that you are able to kill them. So how could I make them unkillable?

10 Likes

I suggest that you put this script (NOT A LOCALSCRIPT) inside the Humanoids of the NPCs:

while true do
   script.Parent.MaxHealth = math.huge
   script.Parent.Health = math.huge
   wait()
end

The wait() is necessary to prevent the script from timing out.
This will work but better solutions probably exist.

6 Likes

Thank you alot Couageous_Canister!!

You could try inserting a forcefield object and setting the visible property to false so you don’t need a loop.

1 Like

Change the title, it doesn’t make sense according to the Body of the post

For future reference: do not use a loop for this because an event exists for detecting changes to health which you can then undo. If there’s an event, use it over a loop.

local currentHealth = Humanoid.Health

Humanoid.HealthChanged:Connect(function ()
    Humanoid.Health = currentHealth
end)

This locks the Humanoid’s health at its current value.

4 Likes

I have tried this script but when I put the script in the npc’s humanoid it underlines everything saying Humanoid in the script. When I hover over it it says: " W001: Unknown global ‘humanoid’ "

Yeah, because it’s not a complete code sample. You aren’t supposed to copy and paste it directly into your own code and then just run it without making needed changes yourself.

1 Like

I understand but I know nothing of roblox lua. are there any good guide’s?

Do some research on your own. There’s also an overabundance of threads here on the DevForum about “any resources for learning programming” or “how to learn programming” or whatever. The search bar is (should be) your first and best friend for a majority of, if not all, questions you may have.

2 Likes

Just put a forcefield in them and set visible to false. This is the easiest solution, and you don’t need any scripts.

11 Likes

Do I have to put the forcefield in humanoid?

1 Like

No, you should put it into the model.

1 Like

However, forcefields only protect the humanoid against :TakeDamage()

If a kill script has Humanoid.Health = 0, the forcefield won’t help.

Thanks, but the forcefield idea prom pewpie works. Another problem is that you can push the npc’s.

Try anchoring the HumanoidRootPart

I’ll test it when I’m on roblox studio.

You can just anchor all character’s parts. Unanchored parts hurt performance a bit.

By the way, if you want to keep forcefields, and you see the forcefield don’t work with some swords/guns, then imagine that this is the line where you damage the player:

Bullet.Touched:connect(function(hit)
   hit.Humanoid.Health = 0
end)

I mean, this is an example, if the forcefield doesn’t works, then add this line in the code:

Bullet.Touched:connect(function(hit)
    if hit.Forcefield:IsA("Forcefield") then
        hit.Humanoid.Health = 0
    end
end)

I hope this helps!

Also, if you want to instead of making a forcefield item, and make a forcefield with a value, then do this:

Bullet.Touched:connect(function(hit)
    local Forcefield = hit:FindFirstChild("InfiniteHealth")
    if Forcefield.Value = 1 then 
    --If you want to keep this as a bool value, then write
    --"If Forcefield.Value then"
        hit.Humanoid.Health = 0
    end
end)

You’re going to get a lot of errors with that code.
The parameter that gets passed into the Touched event is the basepart that touched the part with the event listener. You’re trying to index a “Humanoid” item inside the part without checking if it exists first, and an NPC’s humanoid object would be a direct descendant of the NPC model, not any of its parts.
The same problem is in the second piece of code because you’re calling a method (IsA) on something that might not exist.
Also, :connect is deprecated, use :Connect instead.
A forcefield seems like the best way to do this since there’s no script needed to monitor the NPC’s health. Anything that damages humanoids should be using :TakeDamage anyway.