Humanoids Not Taking Damage?

Hey there!

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
1 Like

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()

1 Like

Any solutions on how to fix the game.Players:GetChildren()?

Check in workspace to find npcs using

game.Workspace:GetChildren()

Well you need to iterate through two separate tables – Players and NPCs.

I would start by creating a table that contains all NPCs such as

local npcTable = {}
for i,v in workspace:GetChildren() do
if v:GetAttribute("NPC") == true then
table.insert(npcTable, v)
end
end

And the same thing for players

local players = game.Players:GetPlayers()

Note: Use :GetPlayers() not :GetChildren() as you want to return only players, not anything else that might appear inside of game.Players.

And then just redo your code, catering for both tables. You could even merge the tables for simplicity if you wanted to.

Let me know if you get stuck.