In my game, I have several NPC that is very important to the game, that should not be killed. Therefore, I am trying to make them immortal so no one can kill them. I tried 2 methods listed below, but all of these seems not to be able to protect npc from directly setting humanoid`s health to 0 or breaking joints. Is there anyway to make them completely immortal, or even lock the property of humanoid so it is unchangeable?
--method 1 : npc.Humanoid.MaxHealth = math.huge npc.Humanoid.Health = math.huge --method2 while wait() do npc.Humanoid.Health = math.huge end
Above answers kind of work but are not guaranteed, as soon as you take damage in between the waits and it's over 100 damage you'll still die.
The only way to fully stop it is from applying an infinite invisible forcefield on the players and rewriting all your code to use Humanoid:TakeDamage()
instead of setting Humanoid.Health
directly. That way the forcefield soaks up the damage.
Edit: https://developer.roblox.com/en-us/api-reference/class/ForceField
Edit 2: Breaking joints cannot be prevented, limbs etc are fine against breaking joints, only if the joint between the body and head is broken the NPC will die. But usually explosions won't break these joints.
This method doesn't prevent the npc from getting killed, but it does spawn it back in where it was last positioned.
-- this scripts parent should be the humanoid while true do script.Parent.HealthChanged:Wait() local charClone = script.Parent.Parent:Clone() if script.Parent.Health <= 0 then charClone.Parent = workspace charClone.Humanoid.Health = charClone.Humanoid.MaxHealth charClone:MakeJoints() script.Parent.Parent:Destroy() end end
math.huge is not the best way for this. Try this:
local NPC = workspace.NPC or workspace:WaitForChild("NPC") local maxhp = NPC.Humanoid.MaxHealth while true do NPC.Humanoid.Health = maxhp wait(0.5) end
This should be a normal (server) script in the workspace. This will make the NPC's health max every 0.5 seconds