Is it possialbe to make an AI consistantly find a path to a player?

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

To make a pathfinding AI constantly move towards the players new position.

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

Not sure how this is possiable to make this work without breaking the pathfinding system.

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

I have recenlty got my AI to naviget towards a players position, but in order for the AI to keep following it has to reach the location of the where the player was last.
Is there a way to have the pathfinding service constantly update the postion of the player before it reaches the old position?

this is the code block I’ve figured out so far:

local function Capture(target)
	local distance = (Root.Position - target.HumanoidRootPart.Position).Magnitude
	local NewPath = Pathfinding:CreatePath({
		["AgentHight"] = 4,
		["AgentRadius"] = 5,
		["AgentCanClimb"] = true,
		["AgentCanJump"] = true,
		
		Costs = {
			Water = math.huge
		}
	})
	
	NewPath:ComputeAsync(Root.Position, target.HumanoidRootPart.Position)

	if distance > 5 then
		if NewPath.Status == Enum.PathStatus.Success then
			for index, target in pairs(NewPath:GetWaypoints()) do
				Humanoid:MoveTo(target.Position)
				Humanoid.MoveToFinished:Wait(0.01)
			end
		end
	else
		attack(target)
	end
end
1 Like

Update: I have managed to get the NPC to update to the players position, but it does struggle to find it’s way aound corners. and my attack function is a bit buggy. Does anyone know any way I can improve it anymore?

local function attack()
	local Debounce = false
	Entity.Humanoid.Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild("Humanoid") and not Debounce then
			Debounce = true
			Attack:Play()
			wait (1)
			hit.Parent.Humanoid.Health -=5
			Attack:Stop()
			Debounce = false
		end
	end)
end

local function Capture(target)
	local distance = (Root.Position - target.HumanoidRootPart.Position).Magnitude
	local counter = 0
	
	RunSerivce.Heartbeat:Connect(function(step)
		
		counter += step
		
		if counter >= 1 then
			
			counter = 0
			
			local NewPath = Pathfinding:CreatePath({
				["AgentHight"] = 4,
				["AgentRadius"] = 5,
				["AgentCanClimb"] = true,
				["AgentCanJump"] = true,

				Costs = {
					Water = math.huge
				}
			})

			NewPath:ComputeAsync(Root.Position, target.HumanoidRootPart.Position)

			if distance > 5 then
				if NewPath.Status == Enum.PathStatus.Success then
					for index, target in pairs(NewPath:GetWaypoints()) do
						if not Walk.IsPlaying then
							Walk:Play()
						end
						Humanoid:MoveTo(target.Position)
						Humanoid.MoveToFinished:Wait(0.01)
					end
				else
					Humanoid:MoveTo(target.Position - (Root.CFrame.LookVector * 10))
				end
			else
				if Walk.IsPlaying then
					Walk:Stop()
				end
				attack(target)
				wait(2)
			end	
		end
	end)
end

Also here is an example of the NPC having issues with corners:
Crawler Showcase002

Increase your agent radius a bit more?

1 Like

I’ll see if that works. As that might fix the whole bumping into corners.

Also I think I need to fix a few things with the whole script as once the AI kills me it goes into a weird loop and lags out the game.

I feel like it might be to do with my attack function.