Pathfinding doesnt patrol after losing sight of the player

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 :broken_heart:

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)

NVM I WAS DUMB!!! the simplest solution was to change order of the code
at the same time it might be a bandaid instead of a solution…

game:GetService('RunService').Heartbeat:Connect(function()
	local target = findTarget()
	
	if target ~= nil and canSeeTarget(target) then
		if target.Humanoid.Health > 0 then
			print('Target', target.Name)
			attack(target)
			wait()
		end
	else
		target = nil
		print('Patrol')
		patrol()
	end
end)
1 Like

Hey there @CarrotusPrime,

RunService.Heartbeat is a solution to this for sure, but you could handle this through creating an invisible ProximityPrompt and then using a LocalScript firing a remote when the prompt is shown or hidden to the player, see doc reference below.

The ProximityPrompt can be adjusted through the bool property RequiresLineOfSight and the number property MaxActivationDistance.

To validate on the server, you could have an approach of checking the magnitude or something similar.

This is of course a suggestion on what you could potentially do, however it does not come without its risks due to being managed by the client. What you have currently may be more safer and secure.

1 Like