So I made this script to make an NPC punch when it gets within a certain distance of the player but the problem is that when it punches it doesn't stop moving towards the player which makes it hard to evade the punch. So how would I get it to stop when it punches the player?
Here is the code:
local larm = script.Parent:FindFirstChild("HumanoidRootPart") local rarm = script.Parent:FindFirstChild("HumanoidRootPart") local punchdist = 10 local animation = Instance.new("Animation") animation.AnimationId = "http://www.roblox.com/Asset?ID=02568852350" local Humanoid = script.Parent:FindFirstChild("Robot") animTrack = Humanoid:LoadAnimation(animation) local CanPunch = true function punch() if CanPunch then CanPunch = false animTrack:Play() wait(0.75) animTrack:Stop() CanPunch = true end end function findNearestTorso(pos) local list = game.Workspace:children() local torso = nil local dist = 100 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(1) local target = findNearestTorso(script.Parent.HumanoidRootPart.Position) if target ~= nil then script.Parent.Robot:MoveTo(target.Position, target) if punchdist >= (target.Position - script.Parent.HumanoidRootPart.Position).magnitude then punch() end end end
NVM I found out what I did wrong & I fixed it.