Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How To Make NPC Immortal from setting health property or breaking joints?

Asked by
Yuuwa0519 197
4 years ago
Edited 4 years ago

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


0
Couldn't you just add an if statement to check to see if the Humanoid is an npc (using either.Name, or the location, or better yet put them all in a folder and check the folder)? If it is, then you can do whatever you want (0 damage display, gui that says something). SteelMettle1 394 — 4y

3 answers

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

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.

0
Good answer but "Above answers kind of work but are not guaranteed", explain how my answer isn't guaranteed? In my answer even if the attack were to go over the max health it'd still respawn the npc. ManlikeAzan 90 — 4y
0
@ManLikeAzan It was more meant for the answer of WoTrox however yours would work, though it's more like a way to circumvent a problem rather than a direct solution to a problem User#834 0 — 4y
0
Thanks For Your Help :D I think i will try to now change the damage to Humanoid:TakeDamage Instead Setting Property. Yuuwa0519 197 — 4y
Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

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
0
Thankyou for helping me. I think I will be apply this to my other project :D Yuuwa0519 197 — 4y
Log in to vote
0
Answered by
WoTrox 345 Moderation Voter
4 years ago

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

0
This is not a good method, OP's script snippet that was provided works better than the script you provided. ManlikeAzan 90 — 4y

Answer this question