(Keep in mind this was a NPC I found in toolbox) I keep getting the error "TakeDamage is not a valid member of Folder" from line 22. The folder it's referring to is located in the NPC's model, however there are no parts within this folder and none of the code refers to the folder. This error usually happens when the NPC's fighting another NPC. After the error, the script breaks and doesn't damage the enemy like intended.

TL;DR: I have a script that detects when the sword hits another NPC. However, when the NPC is fighting an enemy, the script stops working and returns with the above error.

Below is the code:

local damage = 10

sword = script.Parent.Handle

debounce = false





function swordTouched(hit)
	if (hit.Parent == nil) then return end
	if debounce == false then debounce = true
	local humanoid = hit.Parent:findFirstChild("Blue")  --Looks for Humanoid in attacked NPC
	local vCharacter = Tool.Parent --Character as in the attacked NPC
	local hum = vCharacter:findFirstChild("Red") --Looks for Humanoid in ATTACKING NPC
	if humanoid~=nil and humanoid ~= hum and hum ~= nil then
		local right_arm = vCharacter:FindFirstChild("Right Arm") 
		if (right_arm ~= nil) then
			local joint = right_arm:FindFirstChild("RightGrip")
			if (joint ~= nil and (joint.Part0 == sword or joint.Part1 == sword)) then
				humanoid:TakeDamage(10)
				local randomNoise = math.random(1,2)--Just a sound randomizer
				if randomNoise == 1 then
				script.Parent.Parent.Head.s1:Play()
				else script.Parent.Parent.Head.s2:Play() 
				end
				wait(1) --allows for the sword to be used again by NPC
			end
		end
	end
	debounce = false
	end
end