Universal Script 📌
1.0k Views
463 Executions

Pathfinding and follow script

Universal Script 📌
ScriptDAILY
ScriptDAILYPublished 5
9 months ago
Get an executor
Please login to perform these actions:

Does it work?

No reports yet

No reports yet. Tried it? Be the first to say.

Also noticed:

Description

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

View Raw
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


Comments

0
Log in to join the conversation. Login
No comments yet. Be the first!

Tags

More universal scripts

See all
Script hub
46.4k
11.5k
Bumped 2 hours ago
Key System
Paid
Script hub
2.5M
798.8k
Bumped 3 hours ago
Key System

Script hub

#9 today

Verified script badgeLumin Hub | #1 | Insta Steal