NPC Sword Ai instantly killing next targets

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    Create a NPC Ai welding a Sword

  2. What is the issue? Include screenshots / videos if possible!
    When the NPC attacks the first NPC, it does take 30 damage every 2 seconds, but then the other
    targets get killed instantly when the sword touches them

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I’ve looked for something on Developer Hub and couldn’t find anything

Here is the script

local zomb = script.Parent
local zombHum = zomb:WaitForChild("Humanoid")
local zombTorso = zomb:WaitForChild("Torso")
local PFS = game:GetService("PathfindingService")
local slashDebounce = false




local function findTarget()
	local distance = 100
	local target = nil
	for _,v in pairs(workspace:GetChildren()) do
		local hum = v:FindFirstChild("Humanoid")
		local torso = v:FindFirstChild("Torso")
		if hum and torso and v ~= script.Parent then
			if hum.Health ~= 0 then
				
				if (zombTorso.Position - torso.Position).magnitude < distance then
					print("Found Player")
					distance = (zombTorso.Position - torso.Position).magnitude
					target = torso
					
					
					
				end
			else	
				continue
				
			end
				
			else
				local newHumanoid = v:FindFirstChild("Humanoid")
				local newTorso = v:FindFirstChild("Torso")
				if newHumanoid and newTorso and v ~= script.Parent then
					if (zombTorso.Position - newTorso.Position).magnitude < distance then
						print("Found Player")
						distance = (zombTorso.Position - newTorso.Position).magnitude
						target = newTorso
				end
				
			end	
		end
	end
	return target
end

local function slash()
	if not slashDebounce then
		slashDebounce = true
		local humanoidDamaged = {}
		local animation = zombHum:LoadAnimation(zombHum.SlashAnimation)
		animation:Play()
		if animation.IsPlaying then
			zomb.Tool.Part.Touched:Connect(function(hit)
				if hit.Parent:FindFirstChild("Humanoid") and hit.Parent ~= zomb and humanoidDamaged[table.find(humanoidDamaged,hit.Parent.Humanoid)] == nil then
					table.insert(humanoidDamaged,hit.Parent.Humanoid)
					hit.Parent.Humanoid:TakeDamage(30)
					if hit.Parent.Humanoid.Health == 0 then
						animation:Stop()

					end
				end
			end)

		else 
			animation:Play()
			zomb.Tool.Part.Touched:Connect(function(hit)
				if hit.Parent:FindFirstChild("Humanoid") and hit.Parent ~= zomb and humanoidDamaged[table.find(humanoidDamaged,hit.Parent.Humanoid)] == nil then
					table.insert(humanoidDamaged,hit.Parent.Humanoid)
					hit.Parent.Humanoid:TakeDamage(30)
					if hit.Parent.Humanoid.Health == 0 then
						animation:Stop()

					end
				end

			end)


		end
	end
	wait(2)
	slashDebounce = false
end







while wait() do
	local torso = findTarget()
	if torso then
		local path = PFS:CreatePath()
		path:ComputeAsync(zombTorso.Position,torso.Position)
		
		for _, waypoint in pairs(path:GetWaypoints()) do
			if waypoint.Action == Enum.PathWaypointAction.Jump then
				zombHum:ChangeState(Enum.HumanoidStateType.Jumping)
			end
			zombHum:MoveTo(waypoint.Position)
			zombHum.MoveToFinished:Wait()
		end
		
		if (zombTorso.Position - torso.Position).magnitude < 5 then
			slash()
		end
		
	else
		zombHum.MoveToFinished:Wait()
		wait(1)
		zombHum:MoveTo(zombTorso.Position + Vector3.new(math.random(-100,100),0,math.random(-100,100)))
		
	end
end

1 Like