NPCs damage each other

Hello, I have a script that damages Players but it also damages other NPCs which is really a problem, ive searched some answers to this problem but none of them are helpful except the Players:GetPlayerFromCharacter(parent) part but I don’t really know what to do after this.
Script:

local debounce = false
local virus = script.Parent

local function infected(virus)
local parent = virus.parent
	if game.Players:GetPlayerFromCharacter(parent)then
		debounce = true
			while banana == true do
			parent.Humanoid:TakeDamage(0.1)
			wait(1)
		end
	end
end


virus.TouchEnded:Connect(function(hit)
	local Humanoid = hit.Parent:FindFirstChildWhichIsA("Humanoid")
	if Humanoid then
		debounce = false
	end
end)

virus.Touched:Connect(infected)
1 Like

how about you put a value you in the player character and then the npc checks for it and if it does find it do the damage

It seems to me like you’ve solved your own problem. What exactly is going wrong with your script?

They would still damage each other, this is also infact one of the stuff i read while finding an answer.

Ah, I think I’ve found your problem.

local debounce = false
local virus = script.Parent

local function infected(part) -- this parameter should be the part triggering the touched event
    local parent = part.parent -- potentially a player, or an NPC, or something else

    -- combined conditional that filters out everything but players
	if parent:FindFirstChild("Humanoid") and game.Players:GetPlayerFromCharacter(parent) then
		-- do damaging stuff
	end
end


virus.TouchEnded:Connect(function(hit)
	local Humanoid = hit.Parent:FindFirstChildWhichIsA("Humanoid")
	if Humanoid then
		debounce = false
	end
end)

virus.Touched:Connect(infected)

Try that and see if it works.

It did’nt since all NPCs require “Humanoids” to function which basically makes that addition a bit useless.

I don’t think you’ve utilized the changes I suggested.

if parent:FindFirstChild("Humanoid")

This part of the code determines if the Touched event triggerer contains a humanoid.

and game.Players:GetPlayerFromCharacter(parent)

This part of the code ensures that said humanoid belongs to an actual player. For an NPC, this value would be nil, and thus the condition does not pass.

What part of this isn’t working for you?

1 Like