Pathfinding problem

I wanna write a script for ability. Its gonna work like when the player press E and his mouse.Target = another player then he runs to the target himself and attacks when he reaches him. I tried MoveTo command but when the target is running from u u easily getting stucked because of walls, corridors corners and etc. But i want the player could chase the target no matter what, except situations when the target is toooooooo far from the player

Try using the PathfindingService:CreatePath method to calculate a path to the target and use MoveTo for each waypoints, if he gets stuck in the corners then try increasing the AgentParameters.AgentRadius property.

If the target is moving, you’ll need to recalculate a path continuously (like every 0.2s for example).
As for the player being too far, just check the distance magnitude to decide when it’s too far:

local MAX_DISTANCE = 200
local distance = (playerRootPart.Position - targetRootPart.Position).Magnitude
if distance > MAX_DISTANCE then
  -- stop following this target
end

Sometimes, generated paths might fail or get blocked, you’ll also need to handle waypoints and their actions. I suggest you either make your own pathfinding system to handle all that, or use an open-source module like SimplePath which probably has the things you need.

You should use PathfindingService for this kind of feature.
There are several tutorials on YouTube (for example, this one) that explain how to set it up — even though that video is a bit old, there are many newer ones too.
You’ll need to modify the pathfinding logic so that it triggers when pressing E, and also temporarily disables player controls while the character is following the path.

Here’s a simple version

local PathfindingService = game:GetService("PathfindingService")
local UserInputService = game:GetService("UserInputService")
local ContextActionService = game:GetService("ContextActionService")
local Player = game:GetService("Players").LocalPlayer
local path = PathfindingService:CreatePath({
	AgentRadius = 5,
	WaypointSpacing = 6,
})

-- Disables player movement temporarily
local function deactivateMovement(enabled)
	if enabled then
		ContextActionService:BindAction("deactivateMovement", function() end, false,
			Enum.KeyCode.W, Enum.KeyCode.A, Enum.KeyCode.S, Enum.KeyCode.D,
			Enum.KeyCode.Up, Enum.KeyCode.Left, Enum.KeyCode.Right, Enum.KeyCode.Down,
			Enum.KeyCode.Space
		)
	else
		ContextActionService:UnbindAction("deactivateMovement")
	end
end

-- Gets the mouse position in 3D space
local function getMousePosition()
	local mouse = UserInputService:GetMouseLocation()
	local ray = workspace.CurrentCamera:ViewportPointToRay(mouse.X, mouse.Y)
	return workspace:Raycast(ray.Origin, ray.Direction * 100)
end

-- Press E to move toward the mouse target
UserInputService.InputBegan:Connect(function(input)
	if input.KeyCode ~= Enum.KeyCode.E then return end
	if not Player.Character or not Player.Character.PrimaryPart then return end

	local result = getMousePosition()
	if not result then return end

	path:ComputeAsync(Player.Character.PrimaryPart.Position, result.Position)
	if not path then return end

	local humanoid = Player.Character:FindFirstChildOfClass("Humanoid")
	if not humanoid then return end

	humanoid.WalkSpeed = 80
	deactivateMovement(true)

	for _, waypoint in path:GetWaypoints() do
		humanoid:MoveTo(waypoint.Position)
		humanoid.MoveToFinished:Wait()
	end

	deactivateMovement(false)
	humanoid.WalkSpeed = 16
end)

Keep in mind that you need to:

  • Continuously recalculate the path if the target is moving
  • Add error handling for when ComputeAsync fails