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.
Let's use a debounce and use spawn for our cooldown
I'll create variable called db and set it to true, when the attack happens i will check if it is true, if so setting it to false and then using spawn to put waiting a certain amount of time (cooldown) and setting db to true in another thread.
I am assuming that the code after
if (temp.Position - pos).magnitude <= 4 then
is the attack
--my variable local db=true 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 --i add check for db if (temp.Position - pos).magnitude <= 4 and db==true then --set db to false db=false 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() --new thread using spawn spawn(function() --cooldown time wait(5) --set db to true db=true end) end end end end end return torso end while true do wait(1) local target = findNearestTorso(script.Parent.Parent.HumanoidRootPart.Position) if target ~= nil then wait(0.2) script.Parent.Humanoid:MoveTo(target.Position, target) end end