With this current script, the NPC just spams the attack over and over. I tried to keep it from spamming but I can never get it to work. The spam also keeps the slash sound from playing after the first time.
local larm = script.Parent.Parent:FindFirstChild("Left Arm")
local rarm = script.Parent.Parent:FindFirstChild("Right Arm")
function findNearestTorso(pos)
local list = game.Workspace:children()
local torso = nil
local dist = 30
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.Parent) then
temp = temp2:findFirstChild("HumanoidRootPart")
human = temp2:findFirstChild("Humanoid")
if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then
local cool = false
if (temp.Position - pos).magnitude < dist then
torso = temp
dist = (temp.Position - pos).magnitude
end
if cool == false then
if (temp.Position - pos).magnitude <= 4 then
cool = true
local anim = Instance.new("Animation")
anim.Name = "Slash"
anim.AnimationId = "http://www.roblox.com/Asset?ID=27432559"
anim.Parent = script.Parent.Parent
local track = script.Parent.Parent.Humanoid:LoadAnimation(anim)
track:Play(0)
script.Parent.Handle.SwordSlash:Play()
end
end
end
end
end
return torso
end
while true do
wait(1)
local target = findNearestTorso(script.Parent.Parent.HumanoidRootPart.Position)
end
Edit: Removed questionable unused script. Edit 2: Found out that he never stops attacking even after I move way away.

3 comments
this isnt the attacking script
this is just a find nearest player and move to them script
I didn't mean to paste the bottom part. That doesn't actually happen. I'm using the findNearestTorso so that the NPC knows when to attack.