NPC should be able to go back to patrolling after losing the player
When the player leaves, it just stays still and doesnt go patrolling anymore, like its head over heels locked on the player
It waits for the player to come back
heres a segment of the code!!
local function attack(target)
if target and target.Humanoid.Health > 0 then
local playerDistance = (target.HumanoidRootPart.Position-rootPart.Position).Magnitude
if not canSeeTarget(target) then
createPath(target.HumanoidRootPart.Position - (rootPart.CFrame.LookVector*10))
return
else
createPath(target:WaitForChild("HumanoidRootPart").Position + (target:WaitForChild("HumanoidRootPart").Velocity.Unit * 7))
end
if playerDistance <= 3 and canSeeTarget(target) then
target.Humanoid.Health -= 1
task.wait(1)
end
end
end
function createPath(destination)
local path = getPath(destination)
local waypoints
local distance
if path.Status == Enum.PathStatus.Success then
target = findTarget()
waypoints = path:GetWaypoints()
for i, waypoint in pairs(waypoints) do
if waypoint.Action == Enum.PathWaypointAction.Jump then
humanoid.Jump = true
end
humanoid:MoveTo(waypoint.Position)
repeat
distance = (waypoint.Position - rootPart.Position).magnitude
wait()
until distance <= 5
end
else
target = nil
createPath(destination - (rootPart.CFrame.LookVector*10))
end
end
game:GetService('RunService').Heartbeat:Connect(function()
target = findTarget()
if target ~= nil and target.Humanoid.Health > 0 then
if canSeeTarget(target) then
print('Target', target.Name)
attack(target)
wait()
end
else
target = nil
print('Patrol')
patrol()
end
end)