Pathfinding and follow script
Key RequiredUniversal

Pathfinding and follow script

Universal ScriptScriptDAILYScriptDAILY 936 views 297 executesUpdated Feb 11, 2026
Unrated
00
Lua Script•105 lines (3535 chars)
local PathfindingService = game:GetService("PathfindingService")
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local UserInputService = game:GetService("UserInputService")

local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local rootPart = character:WaitForChild("HumanoidRootPart")

local targetPlayer = nil
local currentHighlight = nil
local path = PathfindingService:CreatePath({
	AgentRadius = 2,
	AgentCanJump = true,
})

-- Function to clean up following state
local function stopFollowing()
	targetPlayer = nil
	if currentHighlight then
		currentHighlight:Destroy()
		currentHighlight = nil
	end
	humanoid:MoveTo(rootPart.Position) -- Stop moving
end

-- Function to follow the target using Pathfinding
local function followTarget()
	while targetPlayer and targetPlayer.Character and targetPlayer.Character:FindFirstChild("HumanoidRootPart") do
		local targetRoot = targetPlayer.Character.HumanoidRootPart
		local destination = targetRoot.Position
		
		-- Only compute path if target is far enough away
		if (rootPart.Position - destination).Magnitude > 5 then
			local success, errorMessage = pcall(function()
				path:ComputeAsync(rootPart.Position, destination)
			end)

			if success and path.Status == Enum.PathStatus.Success then
				local waypoints = path:GetWaypoints()
				-- Move to the second waypoint (the first is usually the current position)
				if waypoints[2] then
					humanoid:MoveTo(waypoints[2].Position)
					if waypoints[2].Action == Enum.PathWaypointAction.Jump then
						humanoid.Jump = true
					end
				end
			else
				-- Fallback if pathfinding fails: move directly
				humanoid:MoveTo(destination)
			end
		end
		task.wait(0.1) -- Refresh rate for path recalculation
	end
	stopFollowing()
end

-- Detect Taps/Clicks
UserInputService.InputBegan:Connect(function(input, gameProcessed)
	if gameProcessed then return end
	
	if input.UserInputType == Enum.UserInputType.MouseButton1 or input.UserInputType == Enum.UserInputType.Touch then
		local mousePos = UserInputService:GetMouseLocation()
		local unitRay = workspace.CurrentCamera:ViewportPointToRay(mousePos.X, mousePos.Y)
		local raycastParams = RaycastParams.new()
		raycastParams.FilterType = Enum.RaycastFilterType.Exclude
		raycastParams.FilterDescendantsInstances = {character}
		
		local result = workspace:Raycast(unitRay.Origin, unitRay.Direction * 500, raycastParams)
		
		if result and result.Instance then
			local hitCharacter = result.Instance:FindFirstAncestorOfClass("Model")
			local hitPlayer = Players:GetPlayerFromCharacter(hitCharacter)
			
			if hitPlayer then
				-- If tapping the SAME player, stop following
				if hitPlayer == targetPlayer then
					stopFollowing()
				else
					-- If tapping a NEW player, swap targets
					stopFollowing()
					targetPlayer = hitPlayer
					
					-- Create Red Outline
					currentHighlight = Instance.new("Highlight")
					currentHighlight.FillTransparency = 1 -- Only outline
					currentHighlight.OutlineColor = Color3.fromRGB(255, 0, 0)
					currentHighlight.OutlineTransparency = 0
					currentHighlight.Parent = hitCharacter
					
					-- Start following loop in a separate thread
					task.spawn(followTarget)
				end
			end
		end
	end
end)

-- Stop following if the target leaves the game
Players.PlayerRemoving:Connect(function(playerWhoLeft)
	if targetPlayer and playerWhoLeft == targetPlayer then
		stopFollowing()
	end
end)

Features & Information

Pathfind a player. When they are clicked you follow them and they have a red outline only appears for you
#Pathfind

Community Comments (0)

No comments yet for this script.

More Scripts for Universal Scripts