Unable to get NPC to do damage to other NPCs

  1. What do you want to achieve? Keep it simple and clear!
    Essentially I’m trying to make a tycoon game where a player’s NPC targets other NPCs which are grouped in a folder from a spawn. The NPC targets the enemy NPCs but doesn’t do any damage.

  2. What is the issue? Include screenshots / videos if possible!

Script:

local npc = script.Parent
local animation = script:WaitForChild('SwordAnimation')
local humanoid = script.Parent:WaitForChild('Humanoid')

local animator = humanoid.Animator
local swordAnimation = animator:LoadAnimation(animation)

local sword = npc.Sword
local damagePart = sword.DamagePart
local damage = 10

--local function onTouch(otherPart) -- otherPart = Humanoid
--	local humanoid = otherPart.Parent:FindFirstChild("Humanoid")
--	if humanoid then
--		humanoid -= damage
--	end
--end

local function onTouch() -- otherPart = Humanoid
	local enemyNpcs = workspace.TestFolder:GetChildren()
	if enemyNpcs.className == "Model" then
		enemyNpcs:FindFirstChild("Humanoid"):TakeDamage(10)
	end
end

function findNearestTorso(pos)
	local list = workspace.TestFolder:GetChildren()
	local torso = nil
	local dist = 1000
	local temp = nil
	local human = nil
	local temp2 = nil
	for x = 1, #list do
		temp2 = list[x]
		if (temp2.className == "Model") and (temp2 ~= script.Parent) then
			temp = temp2:findFirstChild("HumanoidRootPart")
			human = temp2:findFirstChild("Humanoid")
			if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then
				if (temp.Position - pos).magnitude < dist then
					torso = temp
					dist = (temp.Position - pos).magnitude
				end
			end
		end
	end
	return torso
end


while true do
	wait(math.random(1,5))
	local target = findNearestTorso(script.Parent.HumanoidRootPart.Position)
	if target ~= nil then
		script.Parent.Humanoid:MoveTo(target.Position, target)
		swordAnimation:Play()
		if swordAnimation:Play() then
			damagePart.Touched:Connect(onTouch)
		end
	end
end
  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I have tried looking for solutions as well as video but was unable to find any or were not quite what I was looking for.
1 Like

in the ontouch function, u have to loop through enemyNpcs since getchildren returns abtable

1 Like

So I tried putting a while loop it didn’t work and as well as a for loop. I’m only assuming that its somehow not getting the Humanoid.

I tried doing

local function onTouch() -- otherPart = Humanoid
	while workspace.TestFolder:GetChildren() do
		local enemyNpcs = workspace.TestFolder:GetChildren()
		if enemyNpcs.className == "Model" then
			print("Found Humanoid")
			enemyNpcs:FindFirstChild("Humanoid"):TakeDamage(10)
		end
	end
end

As well as

			for _,Target in pairs(workspace.TestFolder:GetChildren()) do
				if Target:FindFirstChild("Humanoid") then
					local humanoid = Target
					print("Found Humanoid")
					humanoid:TakeDamage(10)
				end
			end
local humanoid = Target.Humanoid

Btw you are creating an event connection inside a while loop which is just dumb so move the event connection outside the while loop.

Thanks for the notice, I’ll have that fixed.

why is the humanoid the target? try replacing the humanoid variable with
local humanoid = Target:FindFirstChild("Humanoid")

Ok, I tried it it didn’t work but I got some progress on another script I got the NPCs to take damage but right now all the NPCs in the folder take damage but only once.

wait(1)
local npc = script.Parent
local list = workspace:WaitForChild("TestFolder"):GetChildren()

for i,v in pairs(list) do
	local humanoid = v:WaitForChild("Humanoid")
	if v:FindFirstChild("Humanoid") then
		print("Got list")
		humanoid:TakeDamage(10)
	end
end