Stop NPC's From Killing Each Other

So, I made this script where if an NPC touches a player the player loses some health. That part works fine, but I need a way for the NPC’s not to kill each other. Below is my current script.

function onTouched(hit)
	local human = hit.Parent:findFirstChild("Humanoid")
	if (human ~= nil) then
		human.Health = human.Health - 5
	end
end

script.Parent.Touched:Connect(onTouched)
1 Like

Just throw in a check to make sure it’s hitting a player.

function onTouched(hit)
	local human = hit.Parent:findFirstChild("Humanoid")
	if (human ~= nil) then
        if game.Players:GetPlayerFromCharacter(human.Parent) ~= nil then
	     	human.Health = human.Health - 5
        end
	end
end

script.Parent.Touched:Connect(onTouched)
2 Likes