I want to make an npc thats invincible and cant be affected by scripts which decrease a humanoids health, how can this be done?
if i understood you correctly you want a NPC that cant get killed no matter what.
There is no way that i know of preventing “Humanoid.Health = 0” since that will just kill the NPC no matter what. Although, if you are using “Humanoid:TakeDamage(ValueHere)” you can use this to make the NPC immune to that
NPC.Humanoid.MaxHealth = math.huge
NPC.Humanoid.Health = math.huge
You’d have to either set their Health to math.huge
or hard-code it so they can’t be damaged.
If your encountering this issue your probably coding it wrong. Trust me i know how annoying it is when people say “YoUR JuST CodInG IT WrOnG BrUH” but in this case i truly think… that is actually the problem lol.
Yes you could do math.huge on the health but, its probably best to just add some sort of exclusion for the npcs in question. if its unavoidable that you have to run some sort of damage script on a group of npcs then you can use collection service and an if statement to filter.
First get the tag editor plugin:
then create a tag and add it to the NPC model:
Now whenever you run you damage script, exclude all npcs with the tag of like this.
local CS = game:GetService('CollectionService')
for _, npc in game.Npcs:GetChildren() do
if not CS:HasTag('Invincible') then
npc.Humanoid.Health -= 20
end
end
i can i implement it in this script?
script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
local Bomb = Instance.new("Explosion", script.Parent)
Bomb.BlastPressure = 100000
Bomb.BlastRadius = 10
Bomb.Position = script.Parent.Position
end
end)
since the explosion can kill players, whenever i add the collection service tag check it says argument 2 missing or nil
local CS = game:GetService('CollectionService')
script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
if CS:HasTag(hit, 'Invincible') then
return
end
local Bomb = Instance.new("Explosion", script.Parent)
Bomb.BlastPressure = 100000
Bomb.BlastRadius = 10
Bomb.Position = script.Parent.Position
end
end)
CS:HasTag() takes an object and a string value, if the object has the string value aka ‘tag’ then it will return true else it will return false.
so im basically saying if you return true because the object being tested on IS invincible “if CS:HasTag()” then “return” out of the execution
by calling return im telling the script not to run any of the code underneath it if the condition is met.
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.