How do I make explosions do zero damage to NPCs but damage Players

I’m making a zombie game and theres different types of zombies and I added an Explosive one but I only want it to damage players and not other zombies.

I have tried to do it but to no effect, it just ended up not damaging anything.

Code:

local function customExplosion(position, radius, maxDamage)
local explosion = Instance.new("Explosion")
explosion.BlastPressure = 0
explosion.DestroyJointRadiusPercent = 0
explosion.BlastRadius = radius
explosion.Position = position

local modelsHit = {}

-- listen for contact
explosion.Hit:Connect(function(part, distance)
	local parentModel = part.Parent
	if parentModel then 

		if modelsHit[parentModel] then
			return
		end

		modelsHit[parentModel] = true


		local humanoid = parentModel:FindFirstChild("Humanoid")
		if humanoid then
			if humanoid:FindFirstChild("Zombie") then
				if humanoid:FindFirstChild("Zombie").Value ~= true then
					local distanceFactor = distance / explosion.BlastRadius
					distanceFactor = 1 - distanceFactor
					humanoid:TakeDamage(maxDamage * distanceFactor)
				else
					return nil
				end
			end
		end	
	end
end)

explosion.Parent = game.Workspace

end
2 Likes

I think the only way to achieve something like this would be setting Explosion.DestroyJointRadiusPercent to 0 and checking a player’s magnitude in order to see if, upon explosion, whether or not they’re in the blast radius.

Explosions destroy joints & would destroy the NPCs joints as well, killing them whether or not they’re at math.huge health.

2 Likes

You could probably make a custom explosion effect instead of using ROBLOX’s effect. You can also check if the hit model is a player and not NPC by using:

game.Players:GetPlayerFromCharacter(model)

1 Like