Issues with pathfinding

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

currently im trying to get this “entity” to travel the the last seen player location if the sight of the player is lost

  1. What is the issue? Include screenshots / videos if possible!

after its done traveling too the player, it jsut completely stops and refuses to work for some reason
(demonstrate this by letting it see you then going behind the wall)
test.rbxl (191.5 KB)

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

ive tried disabling and enablinmg tons and tons of things in the script, orinting at every if, printing every single variable, and for some reason i still cant find the issue (im probably just stupid)

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!



--{{ SERVICES }}--
local Players = game:GetService("Players")
local PathfindingService = game:GetService("PathfindingService")

--{{ VARIABLES }}--
local rig = script.Parent
local CanPatrol = true
local TrackingLostPlayer = false
local LastTarget = nil
rig:WaitForChild("BackHead").Pacer:Play()
--{{ FUNCTIONS }}--
local function checkForCharacter(char)
	local HRP = char.HumanoidRootPart

	--checks if a raycast from the monmsters hrp hits the chars hrp
	local raycastParams = RaycastParams.new()
	raycastParams.RespectCanCollide = true
	raycastParams.FilterDescendantsInstances = {rig}
	raycastParams.FilterType = Enum.RaycastFilterType.Exclude
	local raycastResult = workspace:Raycast(rig.HumanoidRootPart.Position, HRP.Position - rig.HumanoidRootPart.Position, raycastParams)

	if raycastResult and raycastResult.Instance:IsDescendantOf(char) then
		return true
	else
		return false
	end

end

local function findNearestPlayer()
	local players = Players:GetPlayers()
	local nearestPlayer = nil
	local maxDistance = 500

	for _, player in pairs(players) do
		if player.Character ~= nil and player.Character:FindFirstChild("HumanoidRootPart") then
			local targetCharacter = player.Character
			local distance = (rig.HumanoidRootPart.Position - targetCharacter.HumanoidRootPart.Position).Magnitude

			if distance < maxDistance  then
				nearestPlayer = targetCharacter
				maxDistance = distance
			end
		end
	end
	return nearestPlayer
end



local function calculatePath(destination)
	local agentParams = {
		["AgentHeight"] = 28,
		["AgentRadius"] = 15,
		["AgentCanJump"] = false
	}

	local path = PathfindingService:CreatePath(agentParams)
	path:ComputeAsync(rig.HumanoidRootPart.Position, destination)
	return path
end

local dolosttrack = true
local donetrack = false
local function TrackLost(destination)
	local path = calculatePath(destination)

	if path.Status == Enum.PathStatus.Success then
		donetrack = false
		for _, waypoint in pairs(path:GetWaypoints()) do
			if dolosttrack then
			rig.Humanoid:MoveTo(waypoint.Position)
			rig.Humanoid.MoveToFinished:Wait()
			end
		end
	else
		if dolosttrack then
		rig.Humanoid:MoveTo(destination - (rig.HumanoidRootPart.CFrame.LookVector * 10))
		end
	end
	donetrack = true
end




local function attack(character)
	local distance = (rig.HumanoidRootPart.Position - character.HumanoidRootPart.Position).Magnitude

	if distance > 5 then
		LastTarget = character.HumanoidRootPart
		rig.Humanoid:MoveTo(character.HumanoidRootPart.Position)
	else
		character.Humanoid.Health = 0
	end
end





local can
local can2
local refresh = false
local other = false
local function walkToDestination(destination,item)
	local path = calculatePath(destination)
	print(TrackingLostPlayer)
	if path.Status == Enum.PathStatus.Success then
		
		for _, waypoint in pairs(path:GetWaypoints()) do
			local nearestPlayer = findNearestPlayer()
			if nearestPlayer and checkForCharacter(nearestPlayer) == true then
				print("e")
				attack(nearestPlayer)
				break
			elseif CanPatrol then
				if LastTarget ~= nil and not LastTarget:IsDescendantOf(workspace.Waypoints) and not TrackingLostPlayer and not refresh then
					print("bro")
					task.spawn(function()
						TrackLost(LastTarget.Position)
					end)
					other = true
					TrackingLostPlayer = true
					CanPatrol = false
					if can then
						task.cancel(can)
					end
					if can2 then
						task.cancel(can2)
					end
					
					
					
					can = task.delay(17,function()
						if TrackingLostPlayer then
							dolosttrack = false
							TrackingLostPlayer = false
							CanPatrol = true
							refresh = true
							other = false
						end
					end)					
					can2 = task.spawn(function()
					repeat task.wait() until donetrack
						if TrackingLostPlayer then
							task.cancel(can)
							dolosttrack = false
							TrackingLostPlayer = false
							CanPatrol = true
							refresh = true
							other = false
						end
					end)
					
				else
					refresh = false
					--print("A")
					LastTarget = item
					rig.Humanoid:MoveTo(waypoint.Position)
					rig.Humanoid.MoveToFinished:Wait()
				end
			end
		end
	else
	--	rig.Humanoid:MoveTo(destination - (rig.HumanoidRootPart.CFrame.LookVector * 10))
	end
end

local function patrol()
	local waypoints = workspace.Waypoints:GetChildren()
	local randomNumber = math.random(1, #waypoints)

	local item = waypoints[randomNumber]
	walkToDestination(item.Position,item)
	task.wait()
	patrol()

end


patrol()

(this code was referenced from a youtube tutorial due to my lack of pathfinding experience)

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

1 Like
local function patrol()
	local waypoints = workspace.Waypoints:GetChildren()
	local randomNumber = math.random(1, #waypoints)

	local item = waypoints[randomNumber]
	walkToDestination(item.Position,item)
	task.wait()
	patrol()

end


patrol()

maybe change that whole thing to this:

task.spawn(function()
  while true do
  	local waypoints = workspace.Waypoints:GetChildren()
	  local randomNumber = math.random(1, #waypoints)

	  local item = waypoints[randomNumber]
	  walkToDestination(item.Position,item)
	  task.wait()
  end

end)
1 Like

tried it and the same thing happens with no changes

1 Like