I have an npc that is meant to go to somewhere, but stop to attack nearby characters. The problem is that after it attacks, it won’t continue its path. Here is the script:
local PathService = game:GetService("PathfindingService")
local path = PathService:CreatePath()
local char = script.Parent
local hum = script.Parent.Humanoid
local runAnim = Instance.new("Animation")
runAnim.AnimationId = "rbxassetid://8690354021"
local swingAnim = Instance.new("Animation")
swingAnim.AnimationId = "rbxassetid://8690168171"
local SAnim = script.Parent.Humanoid.Animator:LoadAnimation(swingAnim)
local TAnim = script.Parent.Humanoid.Animator:LoadAnimation(runAnim)
local destination2 = workspace.Destination.Position + Vector3.new(math.random(-(workspace.Destination.Size.X / 2), workspace.Destination.Size.X / 2), 0, 0)
local moveTODONE = true
local wypts
local nxtwypt
local reached
local blocked
local abortPath = false
local db = true
print(destination2)
local function ffollowPath(destination)
local success, err = pcall(function()
path:ComputeAsync(char.PrimaryPart.Position, destination)
end)
if success and path.Status == Enum.PathStatus.Success then
wypts = path:GetWaypoints()
blocked = path.Blocked:Connect(function(blockedWP)
if blockedWP >= nxtwypt then
blocked:Disconnect()
ffollowPath(destination)
end
end)
if not reached then
reached = hum.MoveToFinished:Connect(function(reached2)
if reached and nxtwypt < #wypts then
nxtwypt += 1
if abortPath then return end
if wypts[nxtwypt].Action == Enum.PathWaypointAction.Jump then
hum.Jump = true
else
hum.Jump = false
end
local yes, no = pcall(function()
hum:MoveTo(wypts[nxtwypt].Position)
end)
if not yes then
warn(no)
end
else
print("Path Complete?")
moveTODONE = true
TAnim:Stop()
reached:Disconnect()
blocked:Disconnect()
end
end)
end
nxtwypt = 2
local yess, nos = pcall(function()
hum:MoveTo(wypts[nxtwypt].Position)
end)
if not yess then
warn(nos)
end
TAnim:Play()
else
warn("Path Compute Failed, Error: ", err)
end
end
game:GetService("RunService").Heartbeat:Connect(function()
if db then
for _, thing in ipairs(workspace:GetDescendants()) do
if thing and thing.Parent:FindFirstChildOfClass("Humanoid") and thing.Parent.Parent ~= workspace.Enemy then
if (thing.Parent.PrimaryPart.Position - script.Parent.PrimaryPart.Position).Magnitude <= 10 then
abortPath = true
db = false
abortPath = false
ffollowPath(thing.Parent.PrimaryPart.Position)
print("Following Path")
local TEvent
repeat
if (thing.Parent.PrimaryPart.Position - script.Parent.PrimaryPart.Position).Magnitude <= 5 then
local deebee = true
script.Parent.PrimaryPart.CFrame = CFrame.lookAt(script.Parent.PrimaryPart.Position, thing.Parent.PrimaryPart.Position)
TEvent = script.Parent.Sword.Touched:Connect(function(touched)
if deebee then
deebee = false
if touched.Parent:FindFirstChildOfClass("Humanoid") then
touched.Parent:FindFirstChildOfClass("Humanoid"):TakeDamage(5)
end
wait(0.05)
deebee = true
end
end)
SAnim:Play()
wait(0.25)
SAnim:Stop()
TEvent:Disconnect()
else
abortPath = true
abortPath = false
ffollowPath(thing.Parent.PrimaryPart.Position)
end
wait(0.25)
until (thing.Parent.PrimaryPart.Position - script.Parent.PrimaryPart.Position).Magnitude > 10 or thing.Parent.Humanoid.Health < 1
db = true
abortPath = true
wait(3)
abortPath = false
ffollowPath(destination2)
print(destination2)
print(thing)
break
end
end
end
end
end)
wait(5)
ffollowPath(destination2)
Any help is appreciated!
Thank you!
Feel free to ask for any more information!