I have a grenade script and I want it to damage both player controlled entities and NPCs. It is able to damage players but NPCs are not able to be damaged?
Here is my damage script:
for _, Player in pairs(game.Players:GetChildren()) do
if Player.Character then
local Magnitude = (Player.Character.HumanoidRootPart.Position - clone.Position).magnitude
Magnitude = Magnitude - explosion.BlastRadius
local Damage = 0
if Magnitude <= KillZone then
Damage = 10000
elseif Magnitude < DamageRange then
Damage = 100 - (Magnitude * (100 / DamageRange))
end
Player.Character.Humanoid:TakeDamage(Damage) -- Use TakeDamage method
end
end
Why are you using .Humanoid:TakeDamage()? I think this is the root cause of your issue as TakeDamage takes into account health modifiers such as forcefields.
Instead, use .Humanoid.Health -= Damage. The -= operator is the same as writing .Humanoid.Health = Humanoid.Health - Damage.
Also, upon further review of your script I have noticed that you aren’t even damaging the NPCs at all. You are only damaging Player.Character, NPCs are not players and will not show up in game.Players:GetChildren()