Pathfinding NPC bumps into walls for some reason

I have an NPC that chases you after touching something and for some reason the NPC keeps bumping into walls as if it wasn’t using pathfinding at all. I made the waypoints visible so I can see where it’s supposed to go but it’s ignoring them.


Code:

local module = {}

local PS = game:GetService("PathfindingService")

local function pathFind(enemy, hrp)
	if hrp then
		local humanoid = enemy:WaitForChild("Enemy")
		
		local path = PS:CreatePath()
		path:ComputeAsync(enemy.HumanoidRootPart.Position, hrp.Position)
		
		if path.Status == Enum.PathStatus.Success then
			local points = path:GetWaypoints()
			local part
			for i,v in ipairs(points) do
				humanoid:MoveTo(v.Position)
				part = Instance.new("Part")
				part.Anchored = true
				part.CanCollide = false
				part.Material = "Neon"
				part.Transparency = .5
				part.Position = v.Position
				part.Size = Vector3.new(1,1,1)
				part.Shape = "Ball"
				part.BrickColor = BrickColor.new("Really red")
				part.Name = "Waypoint"
				part.Parent = workspace
			end
			if part then part:Destroy() end
		else
			humanoid:MoveTo(hrp.Position)
		end
	end
end

function module.onTouch(hit, enemy)
	if hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid") then
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		if player and player.Character and player.Character:FindFirstChild("HumanoidRootPart") then
			while true do
				if player.Character:FindFirstChild("HumanoidRootPart") then
					pathFind(enemy, player.Character.HumanoidRootPart)
				else
					break
				end
				task.wait(.25)
			end
		end
	end
end

return module

There’s waypoints in the maze but the NPC ignores them and tries walking into the wall. I’ve tried changing it to pairs and ipairs but nothing works. I don’t know if this is a bug or if I’m doing something wrong.

1 Like

Try Setting up Costs for your Path using , agentParams

1 Like

Still doesn’t work, it’s the same result.

1 Like

Never mind I fixed it, the problem was I didn’t include a ‘.MoveToFinished:Wait()’, so the NPC was skipping to the next waypoint continuously.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.