
1.0k Views
463 Executions
Pathfinding and follow script
Universal Script 📌
ScriptDAILYPublished 5
9 months ago
Please login to perform these actions:Does it work?
No reports yetNo 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
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
0Log in to join the conversation. Login
No comments yet. Be the first!







