Script: local PS = game:GetService("PathfindingService")
local teams = game:GetService("Teams")
local Players = game:GetService("Players")
local RS = game:GetService("ReplicatedStorage")
local Remotes = RS.Remotes
local char = script.Parent
local billy = script.Parent
local hum = billy.Humanoid
local hrp = billy.HumanoidRootPart
local path = PS:CreatePath({
AgentCanJump = false,
AgentCanClimb = true,
})
local maxDistance = 10000
local Event = Remotes.StartPathfind
Event.OnServerEvent:Connect(function()
local nearestPlayer, nearestDistance
for _, player in pairs(Players:GetPlayers()) do
if player.Team == "Killers" then
return
end
local character = player.Character
local distance = player:DistanceFromCharacter(billy.HumanoidRootPart.Position)
if not character or
distance > maxDistance or
(nearestDistance and distance >= nearestDistance)
then
continue
end
nearestDistance = distance
nearestPlayer = player
end
print("The nearest player is ", nearestPlayer)
local Target = nearestPlayer
path:ComputeAsync(hrp.Position, Target.Character.HumanoidRootPart.Position)
if path.Status == Enum.PathStatus.Success then
local waypoints = path:GetWaypoints()
for i, waypoint in waypoints do
local p = Instance.new("Part")
p.Parent = workspace
p.Size = Vector3.new(1,1,1)
p.Position = waypoint.Position
p.Anchored = true
p.CanCollide = false
p.Color = Color3.new(1,0,0)
hum:MoveTo(waypoint.Position)
hum.MoveToFinished:Wait()
end
else
warn("path unsuccessful")
end
end)